-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1523 from DataDog/teodor.todorov/SYNTH-17721/impo…
…rt-ltd-from-a-main-test [SYNTH-17721] Import LTD from a main test
- Loading branch information
Showing
11 changed files
with
713 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/commands/synthetics/__tests__/config-fixtures/import-tests-config-with-all-keys.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"apiKey": "fake_api_key", | ||
"appKey": "fake_app_key", | ||
"configPath": "fake-datadog-ci.json", | ||
"datadogSite": "datadoghq.eu", | ||
"files": [ | ||
"my-new-file" | ||
], | ||
"publicIds": [ | ||
"ran-dom-id1" | ||
], | ||
"testSearchQuery": "a-search-query" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
213 changes: 213 additions & 0 deletions
213
src/commands/synthetics/__tests__/import-tests-lib.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
jest.mock('fs/promises') | ||
import * as fsPromises from 'fs/promises' | ||
|
||
import * as api from '../api' | ||
import {DEFAULT_IMPORT_TESTS_COMMAND_CONFIG} from '../import-tests-command' | ||
import {importTests} from '../import-tests-lib' | ||
import {TriggerConfig} from '../interfaces' | ||
import * as tests from '../test' | ||
|
||
import {getApiTest, getBrowserTest, mockApi, mockReporter} from './fixtures' | ||
|
||
describe('import-tests', () => { | ||
beforeEach(() => { | ||
jest.restoreAllMocks() | ||
jest.mock('fs/promises', () => ({ | ||
writeFile: jest.fn().mockResolvedValue(undefined), | ||
})) | ||
process.env = {} | ||
}) | ||
|
||
describe('importTests', () => { | ||
test('we write imported test to file', async () => { | ||
const filePath = 'test.synthetics.json' | ||
const config = DEFAULT_IMPORT_TESTS_COMMAND_CONFIG | ||
config['files'] = [filePath] | ||
config['publicIds'] = ['123-456-789'] | ||
|
||
const mockTest = getApiTest(config['publicIds'][0]) | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
const {message, monitor_id, status, tags, ...mockTestWithoutUnsupportedFields} = mockTest | ||
const mockLTD = { | ||
tests: [ | ||
{ | ||
local_test_definition: { | ||
...mockTestWithoutUnsupportedFields, | ||
}, | ||
}, | ||
], | ||
} | ||
// eslint-disable-next-line no-null/no-null | ||
const jsonData = JSON.stringify(mockLTD, null, 2) | ||
|
||
const apiHelper = mockApi({ | ||
getTest: jest.fn(() => { | ||
return Promise.resolve(mockTest) | ||
}), | ||
}) | ||
jest.spyOn(api, 'getApiHelper').mockImplementation(() => apiHelper as any) | ||
jest.spyOn(tests, 'getTestConfigs').mockImplementation(async () => []) | ||
|
||
await importTests(mockReporter, config) | ||
|
||
expect(apiHelper.getTest).toHaveBeenCalledTimes(1) | ||
expect(tests.getTestConfigs).toHaveBeenCalledTimes(1) | ||
expect(fsPromises.writeFile).toHaveBeenCalledTimes(1) | ||
expect(fsPromises.writeFile).toHaveBeenCalledWith(filePath, jsonData, 'utf8') | ||
}) | ||
|
||
test('we can fetch multiple public_ids', async () => { | ||
const filePath = 'test.synthetics.json' | ||
const config = DEFAULT_IMPORT_TESTS_COMMAND_CONFIG | ||
config['files'] = [filePath] | ||
config['publicIds'] = ['123-456-789', '987-654-321'] | ||
|
||
const mockTest = getApiTest(config['publicIds'][0]) | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
const {message, monitor_id, status, tags, ...mockTestWithoutUnsupportedFields} = mockTest | ||
const mockLTD = { | ||
tests: [ | ||
{ | ||
local_test_definition: { | ||
...mockTestWithoutUnsupportedFields, | ||
public_id: config['publicIds'][0], | ||
}, | ||
}, | ||
{ | ||
local_test_definition: { | ||
...mockTestWithoutUnsupportedFields, | ||
public_id: config['publicIds'][1], | ||
}, | ||
}, | ||
], | ||
} | ||
// eslint-disable-next-line no-null/no-null | ||
const expectedJsonData = JSON.stringify(mockLTD, null, 2) | ||
|
||
const apiHelper = mockApi({ | ||
getTest: jest | ||
.fn() | ||
.mockReturnValueOnce(Promise.resolve(mockTest)) | ||
.mockReturnValueOnce(Promise.resolve({...mockTest, public_id: config['publicIds'][1]})), | ||
}) | ||
jest.spyOn(api, 'getApiHelper').mockImplementation(() => apiHelper as any) | ||
jest.spyOn(tests, 'getTestConfigs').mockImplementation(async () => []) | ||
|
||
await importTests(mockReporter, config) | ||
|
||
expect(apiHelper.getTest).toHaveBeenCalledTimes(2) | ||
expect(tests.getTestConfigs).toHaveBeenCalledTimes(1) | ||
expect(fsPromises.writeFile).toHaveBeenCalledTimes(1) | ||
expect(fsPromises.writeFile).toHaveBeenCalledWith(filePath, expectedJsonData, 'utf8') | ||
}) | ||
|
||
test('we write browser test', async () => { | ||
const filePath = 'test.synthetics.json' | ||
const config = DEFAULT_IMPORT_TESTS_COMMAND_CONFIG | ||
config['files'] = [filePath] | ||
config['publicIds'] = ['123-456-789'] | ||
|
||
const mockTest = getBrowserTest(config['publicIds'][0]) | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
const {message, monitor_id, status, tags, ...mockTestWithoutUnsupportedFields} = mockTest | ||
const mockLTD = { | ||
tests: [ | ||
{ | ||
local_test_definition: { | ||
...mockTestWithoutUnsupportedFields, | ||
}, | ||
}, | ||
], | ||
} | ||
// eslint-disable-next-line no-null/no-null | ||
const jsonData = JSON.stringify(mockLTD, null, 2) | ||
|
||
const apiHelper = mockApi({ | ||
getTest: jest.fn(() => { | ||
return Promise.resolve(mockTest) | ||
}), | ||
getTestWithType: jest.fn(() => { | ||
return Promise.resolve(mockTest) | ||
}), | ||
}) | ||
jest.spyOn(api, 'getApiHelper').mockImplementation(() => apiHelper as any) | ||
jest.spyOn(tests, 'getTestConfigs').mockImplementation(async () => []) | ||
|
||
await importTests(mockReporter, config) | ||
|
||
expect(apiHelper.getTest).toHaveBeenCalledTimes(1) | ||
expect(apiHelper.getTestWithType).toHaveBeenCalledTimes(1) | ||
expect(tests.getTestConfigs).toHaveBeenCalledTimes(1) | ||
expect(fsPromises.writeFile).toHaveBeenCalledTimes(1) | ||
expect(fsPromises.writeFile).toHaveBeenCalledWith(filePath, jsonData, 'utf8') | ||
}) | ||
|
||
test('we write imported test to already existing file', async () => { | ||
const filePath = 'test.synthetics.json' | ||
const config = DEFAULT_IMPORT_TESTS_COMMAND_CONFIG | ||
config['files'] = [filePath] | ||
config['publicIds'] = ['123-456-789', '987-654-321'] | ||
|
||
const mockTest = getApiTest(config['publicIds'][0]) | ||
|
||
const mockTriggerConfig: TriggerConfig[] = [ | ||
{ | ||
local_test_definition: { | ||
...mockTest, | ||
public_id: 'abc-def-ghi', | ||
}, | ||
}, | ||
{ | ||
local_test_definition: { | ||
...mockTest, | ||
public_id: config['publicIds'][0], | ||
}, | ||
}, | ||
] | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
const {message, monitor_id, status, tags, ...mockTestWithoutUnsupportedFields} = mockTest | ||
const expectedLTD = { | ||
tests: [ | ||
{ | ||
local_test_definition: { | ||
...mockTest, | ||
public_id: 'abc-def-ghi', | ||
}, | ||
}, | ||
{ | ||
local_test_definition: { | ||
...mockTestWithoutUnsupportedFields, | ||
public_id: config['publicIds'][0], | ||
name: 'Some other name', | ||
}, | ||
}, | ||
{ | ||
local_test_definition: { | ||
...mockTestWithoutUnsupportedFields, | ||
public_id: config['publicIds'][1], | ||
}, | ||
}, | ||
], | ||
} | ||
// eslint-disable-next-line no-null/no-null | ||
const expectedJsonData = JSON.stringify(expectedLTD, null, 2) | ||
|
||
const apiHelper = mockApi({ | ||
getTest: jest | ||
.fn() | ||
.mockReturnValueOnce(Promise.resolve({...mockTest, name: 'Some other name'})) | ||
.mockReturnValueOnce(Promise.resolve({...mockTest, public_id: config['publicIds'][1]})), | ||
}) | ||
jest.spyOn(api, 'getApiHelper').mockImplementation(() => apiHelper as any) | ||
jest.spyOn(tests, 'getTestConfigs').mockResolvedValue(mockTriggerConfig) | ||
|
||
await importTests(mockReporter, config) | ||
|
||
expect(apiHelper.getTest).toHaveBeenCalledTimes(2) | ||
expect(tests.getTestConfigs).toHaveBeenCalledTimes(1) | ||
expect(fsPromises.writeFile).toHaveBeenCalledTimes(1) | ||
expect(fsPromises.writeFile).toHaveBeenCalledWith(filePath, expectedJsonData, 'utf8') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.