Skip to content

Commit

Permalink
Merge pull request #933 from JupiterOne/upgrade-jest
Browse files Browse the repository at this point in the history
Upgrade Jest packages
  • Loading branch information
austinkelleher authored Jul 25, 2023
2 parents c00d829 + 87a599f commit ccf0f4f
Show file tree
Hide file tree
Showing 33 changed files with 1,873 additions and 1,835 deletions.
21 changes: 21 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function buildBabelConfig() {
return (api) => {
api.cache(true);

return {
presets: [
[
'@babel/preset-env',
{
targets: {
node: '18',
},
},
],
['@babel/preset-typescript', {}],
],
};
};
}

module.exports = buildBabelConfig();
2 changes: 0 additions & 2 deletions jest.config.base.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// This is a shared base config that is used by all packages
module.exports = {
preset: 'ts-jest',
clearMocks: true,
restoreMocks: true,
testMatch: [
Expand All @@ -9,7 +8,6 @@ module.exports = {
'!**/dist/*',
'!**/*.bak/*',
],
collectCoverage: false,
globals: {
'ts-jest': {
isolatedModules: true,
Expand Down
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,27 @@
"prepush": "yarn lint && yarn build:dist && yarn jest --changedSince main"
},
"devDependencies": {
"@types/jest": "^27.5.1",
"@babel/core": "^7.22.9",
"@babel/preset-env": "^7.22.9",
"@babel/preset-typescript": "^7.22.5",
"@types/jest": "^29.5.3",
"@types/node": "^18",
"@typescript-eslint/eslint-plugin": "^4.29.3",
"@typescript-eslint/parser": "^4.29.3",
"eslint": "^7.24.0",
"eslint-config-prettier": "^8.2.0",
"eslint-plugin-jest": "^24.3.5",
"husky": "^4.2.5",
"jest": "^27.5.1",
"jest-extended": "^0.11.5",
"jest": "^29.6.1",
"jest-extended": "^4.0.0",
"lerna": "^6.0.0",
"lint-staged": "^10.2.6",
"prettier": "^2.2.1",
"ts-jest": "^27.1.5",
"ts-jest": "^29.1.1",
"typescript": "^4.3.5"
},
"resolutions": {
"@types/node": "^18"
},
"version": "0.0.0"
}
1 change: 1 addition & 0 deletions packages/cli/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../babel.config');
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"globby": "^11.0.1",
"jsonexport": "^3.0.1",
"lodash": "^4.17.15",
"ora": "^4.0.4",
"ora": "^5",
"p-all": "^3.0.0",
"rimraf": "^3.0.2",
"upath": "^1.2.0"
Expand Down
52 changes: 31 additions & 21 deletions packages/cli/src/__tests__/cli-export.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import axios from 'axios';
import { mocked } from 'jest-mock';
import * as runtime from '@jupiterone/integration-sdk-runtime';

import { createCli } from '..';
import { TEST_API_KEY, TEST_ACCOUNT } from './utils';
import * as log from '../log';
Expand All @@ -10,12 +8,22 @@ import { createEntity, createRelationship } from '../export/__tests__/utils';

jest.mock('@jupiterone/integration-sdk-runtime');
jest.mock('axios');
jest.mock('ora');
jest.mock('fs');
jest.mock('../log');

const mockedAxios = mocked(axios, true);
const mockedCreateApiClient = mocked(runtime.createApiClient, true);
jest.mock('ora', () => {
return () => {
return {
start: jest.fn().mockReturnThis(),
stop: jest.fn(),
fail: jest.fn(),
succeed: jest.fn(),
};
};
});

const mockedAxios = jest.mocked(axios);
const mockedCreateApiClient = jest.mocked(runtime.createApiClient);

beforeEach(() => {
mockedCreateApiClient.mockReturnValue(axios);
Expand Down Expand Up @@ -65,12 +73,12 @@ test('should export assets', async () => {
]),
).resolves.toEqual(expect.anything());
expect(Object.values(vol.toJSON())).toMatchInlineSnapshot(`
Array [
"[{\\"id\\":\\"entity-1\\",\\"name\\":\\"Entity 1\\",\\"displayName\\":\\"Entity 1\\",\\"createdOn\\":1591831808891,\\"_class\\":\\"Entity\\",\\"_type\\":\\"entity_type_1\\",\\"_key\\":\\"entity-1\\",\\"tag.Production\\":\\"{\\\\\\"object\\\\\\":{\\\\\\"innerProp\\\\\\":\\\\\\"foobar\\\\\\"}}\\"}]",
"[{\\"id\\":\\"entity-2\\",\\"name\\":\\"Entity 2\\",\\"displayName\\":\\"Entity 2\\",\\"createdOn\\":1591831808892,\\"_class\\":\\"Entity\\",\\"_type\\":\\"entity_type_2\\",\\"_key\\":\\"entity-2\\"}]",
"[{\\"_key\\":\\"entity-1|has|entity-2\\",\\"_type\\":\\"entity_type_1_has_2\\",\\"_class\\":\\"HAS\\",\\"_fromEntityKey\\":\\"entity-1\\",\\"_toEntityKey\\":\\"entity-2\\",\\"displayName\\":\\"HAS\\"}]",
[
"[{"id":"entity-1","name":"Entity 1","displayName":"Entity 1","createdOn":1591831808891,"_class":"Entity","_type":"entity_type_1","_key":"entity-1","tag.Production":"{\\"object\\":{\\"innerProp\\":\\"foobar\\"}}"}]",
"[{"id":"entity-2","name":"Entity 2","displayName":"Entity 2","createdOn":1591831808892,"_class":"Entity","_type":"entity_type_2","_key":"entity-2"}]",
"[{"_key":"entity-1|has|entity-2","_type":"entity_type_1_has_2","_class":"HAS","_fromEntityKey":"entity-1","_toEntityKey":"entity-2","displayName":"HAS"}]",
"id,name,displayName,createdOn,_class,_type,_key,tag.Production
entity-1,Entity 1,Entity 1,1591831808891,Entity,entity_type_1,entity-1,\\"{\\"\\"object\\"\\":{\\"\\"innerProp\\"\\":\\"\\"foobar\\"\\"}}\\"",
entity-1,Entity 1,Entity 1,1591831808891,Entity,entity_type_1,entity-1,"{""object"":{""innerProp"":""foobar""}}"",
"id,name,displayName,createdOn,_class,_type,_key
entity-2,Entity 2,Entity 2,1591831808892,Entity,entity_type_2,entity-2",
"_key,_type,_class,_fromEntityKey,_toEntityKey,displayName
Expand All @@ -83,6 +91,7 @@ test('should export assets', async () => {
apiBaseUrl: 'https://api.TEST.jupiterone.io',
});
});

test('should only export relationships when specified', async () => {
mockedAxios.get
.mockResolvedValueOnce({
Expand Down Expand Up @@ -123,13 +132,14 @@ test('should only export relationships when specified', async () => {
]),
).resolves.toEqual(expect.anything());
expect(Object.values(vol.toJSON())).toMatchInlineSnapshot(`
Array [
"[{\\"_key\\":\\"entity-1|has|entity-2\\",\\"_type\\":\\"entity_type_1_has_2\\",\\"_class\\":\\"HAS\\",\\"_fromEntityKey\\":\\"entity-1\\",\\"_toEntityKey\\":\\"entity-2\\",\\"displayName\\":\\"HAS\\"}]",
[
"[{"_key":"entity-1|has|entity-2","_type":"entity_type_1_has_2","_class":"HAS","_fromEntityKey":"entity-1","_toEntityKey":"entity-2","displayName":"HAS"}]",
"_key,_type,_class,_fromEntityKey,_toEntityKey,displayName
entity-1|has|entity-2,entity_type_1_has_2,HAS,entity-1,entity-2,HAS",
]
`);
});

test('should only export entities when specified', async () => {
mockedAxios.get
.mockResolvedValueOnce({
Expand Down Expand Up @@ -170,15 +180,15 @@ test('should only export entities when specified', async () => {
]),
).resolves.toEqual(expect.anything());
expect(Object.values(vol.toJSON())).toMatchInlineSnapshot(`
Array [
"[{\\"id\\":\\"entity-1\\",\\"name\\":\\"Entity 1\\",\\"displayName\\":\\"Entity 1\\",\\"createdOn\\":1591831808891,\\"_class\\":\\"Entity\\",\\"_type\\":\\"entity_type_1\\",\\"_key\\":\\"entity-1\\",\\"tag.Production\\":\\"{\\\\\\"object\\\\\\":{\\\\\\"innerProp\\\\\\":\\\\\\"foobar\\\\\\"}}\\"}]",
"[{\\"id\\":\\"entity-2\\",\\"name\\":\\"Entity 2\\",\\"displayName\\":\\"Entity 2\\",\\"createdOn\\":1591831808892,\\"_class\\":\\"Entity\\",\\"_type\\":\\"entity_type_2\\",\\"_key\\":\\"entity-2\\"}]",
"id,name,displayName,createdOn,_class,_type,_key,tag.Production
entity-1,Entity 1,Entity 1,1591831808891,Entity,entity_type_1,entity-1,\\"{\\"\\"object\\"\\":{\\"\\"innerProp\\"\\":\\"\\"foobar\\"\\"}}\\"",
"id,name,displayName,createdOn,_class,_type,_key
entity-2,Entity 2,Entity 2,1591831808892,Entity,entity_type_2,entity-2",
]
`);
[
"[{"id":"entity-1","name":"Entity 1","displayName":"Entity 1","createdOn":1591831808891,"_class":"Entity","_type":"entity_type_1","_key":"entity-1","tag.Production":"{\\"object\\":{\\"innerProp\\":\\"foobar\\"}}"}]",
"[{"id":"entity-2","name":"Entity 2","displayName":"Entity 2","createdOn":1591831808892,"_class":"Entity","_type":"entity_type_2","_key":"entity-2"}]",
"id,name,displayName,createdOn,_class,_type,_key,tag.Production
entity-1,Entity 1,Entity 1,1591831808891,Entity,entity_type_1,entity-1,"{""object"":{""innerProp"":""foobar""}}"",
"id,name,displayName,createdOn,_class,_type,_key
entity-2,Entity 2,Entity 2,1591831808892,Entity,entity_type_2,entity-2",
]
`);
});

test('should log error when export fails', async () => {
Expand Down
19 changes: 14 additions & 5 deletions packages/cli/src/__tests__/cli-import.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as runtime from '@jupiterone/integration-sdk-runtime';
import axios from 'axios';
import { mocked } from 'jest-mock';
import { vol } from 'memfs';
import { randomUUID as uuid } from 'crypto';
import globby from 'globby';
Expand All @@ -20,15 +19,25 @@ import { createCli } from '..';

jest.mock('@jupiterone/integration-sdk-runtime');
jest.mock('axios');
jest.mock('ora');
jest.mock('fs');
jest.mock('globby');
jest.mock('../pause');
jest.mock('../log');

const mockedCreateApiClient = mocked(runtime.createApiClient, true);
const mockedAxios = mocked(axios, true);
const mockedGlobby = mocked(globby, true);
jest.mock('ora', () => {
return () => {
return {
start: jest.fn().mockReturnThis(),
stop: jest.fn(),
fail: jest.fn(),
succeed: jest.fn(),
};
};
});

const mockedCreateApiClient = jest.mocked(runtime.createApiClient);
const mockedAxios = jest.mocked(axios);
const mockedGlobby = jest.mocked(globby);

const type1Entities = [
createEntity({
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/export/__tests__/bulkDownloadToJson.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as runtime from '@jupiterone/integration-sdk-runtime';
import { mocked } from 'jest-mock';
import axios, { AxiosInstance } from 'axios';
import { randomUUID as uuid } from 'crypto';

Expand All @@ -17,11 +16,12 @@ jest.mock('axios');
jest.mock('@jupiterone/integration-sdk-runtime');
jest.mock('../../fileSystem');

const mockedRuntime = mocked(runtime, true);
const mockedAxios = mocked<AxiosInstance>(axios, true);
const mockedFileSystem = mocked(fileSystem, true);
const mockedRuntime = jest.mocked(runtime);
const mockedAxios = jest.mocked<AxiosInstance>(axios);
const mockedFileSystem = jest.mocked(fileSystem);

const options: BulkDownloadParams = {
apiBaseUrl: '',
account: TEST_ACCOUNT,
apiKey: TEST_API_KEY,
assetType: 'entities',
Expand Down
35 changes: 17 additions & 18 deletions packages/cli/src/export/__tests__/exportAssetsToJson.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import { mocked } from 'jest-mock';
import ora from 'ora';

import {
exportAssetsToJson,
ExportAssetsToJsonParams,
} from '../exportAssetsToJson';
import { exportAssetsToJson } from '../exportAssetsToJson';
import { DEFAULT_EXPORT_DIRECTORY } from '../../commands';
import { bulkDownloadToJson } from '../bulkDownloadToJson';
import * as log from '../../log';
import { TEST_API_KEY } from '../../__tests__/utils';
import { ExportAssetsParams } from '../exportAssets';
import { randomUUID } from 'crypto';

jest.mock('../bulkDownloadToJson');
jest.mock('../../log');
jest.mock('ora');

const mockedBulkDownload = mocked(bulkDownloadToJson, true);
const mockedSpinner = ora().start();
jest.mock('ora', () => {
return () => {
return {
start: jest.fn().mockReturnThis(),
stop: jest.fn(),
fail: jest.fn(),
succeed: jest.fn(),
};
};
});

const mockedBulkDownload = mocked(bulkDownloadToJson);

const options: ExportAssetsToJsonParams = {
const options: ExportAssetsParams = {
account: randomUUID(),
storageDirectory: DEFAULT_EXPORT_DIRECTORY,
includeDeleted: true,
includeEntities: true,
Expand Down Expand Up @@ -47,11 +54,6 @@ test('should download both entities and relationships when specified to include
progress: expect.anything(),
}),
);
expect(mockedSpinner.succeed).toHaveBeenCalledWith(
expect.stringMatching(
/Export Successful, Downloaded \d+ entities and \d+ relationships!/g,
),
);
});

test('should exclude entities when includeEntities is false', async () => {
Expand Down Expand Up @@ -96,7 +98,4 @@ test('should log error when there is an issue bulk downloading', async () => {

await expect(exportAssetsToJson(options)).rejects.toThrow(error);
expect(log.error).toHaveBeenCalledWith('Failed to export assets to JSON');
expect(mockedSpinner.fail).toHaveBeenCalledWith(
'Failed to export assets to JSON',
);
});
30 changes: 23 additions & 7 deletions packages/cli/src/export/__tests__/exportJsonAssetsToCsv.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ora from 'ora';
import { vol } from 'memfs';
import { randomUUID as uuid } from 'crypto';
import { randomUUID, randomUUID as uuid } from 'crypto';
import csvToJson from 'csvtojson';

import { DEFAULT_EXPORT_DIRECTORY } from '../../commands';
Expand All @@ -13,9 +12,18 @@ import { TEST_API_KEY } from '../../__tests__/utils';

jest.mock('fs');
jest.mock('../../log');
jest.mock('ora');

const mockedSpinner = ora().start();
jest.mock('ora', () => {
return () => {
return {
start: jest.fn().mockReturnThis(),
stop: jest.fn(),
fail: jest.fn(),
succeed: jest.fn(),
};
};
});

const TEST_DIRECTORY = DEFAULT_EXPORT_DIRECTORY;
const TEST_FILES = {
[`${TEST_DIRECTORY}/json/entities/entity_type_1/${uuid()}.json`]:
Expand Down Expand Up @@ -55,6 +63,8 @@ beforeEach(() => {

test('should not export anything if there are no json assets', async () => {
await exportJsonAssetsToCsv({
account: randomUUID(),
includeDeleted: false,
includeEntities: true,
includeRelationships: true,
storageDirectory: TEST_DIRECTORY,
Expand All @@ -68,6 +78,8 @@ test('should export both entities and relationships when specified', async () =>
vol.fromJSON(TEST_FILES);

await exportJsonAssetsToCsv({
account: randomUUID(),
includeDeleted: false,
includeEntities: true,
includeRelationships: true,
storageDirectory: TEST_DIRECTORY,
Expand Down Expand Up @@ -111,6 +123,8 @@ test('should export only entities when includeRelationships is false', async ()
vol.fromJSON(TEST_FILES);

await exportJsonAssetsToCsv({
account: randomUUID(),
includeDeleted: false,
includeEntities: true,
includeRelationships: false,
storageDirectory: TEST_DIRECTORY,
Expand Down Expand Up @@ -139,6 +153,8 @@ test('should export only relationships when includeEntities is false', async ()
vol.fromJSON(TEST_FILES);

await exportJsonAssetsToCsv({
account: randomUUID(),
includeDeleted: false,
includeEntities: false,
includeRelationships: true,
storageDirectory: TEST_DIRECTORY,
Expand Down Expand Up @@ -180,16 +196,16 @@ test('should log error when export fails', async () => {

await expect(
exportJsonAssetsToCsv({
account: randomUUID(),
includeDeleted: false,
includeEntities: true,
includeRelationships: true,
storageDirectory: TEST_DIRECTORY,
apiKey: TEST_API_KEY,
}),
).rejects.toThrow(/Unexpected token/g);

expect(log.error).toHaveBeenCalledWith('Failed to export JSON assets to CSV');
expect(mockedSpinner.fail).toHaveBeenCalledWith(
'Failed to export JSON assets to CSV',
);
});

function convertCsvToJson(content: string) {
Expand Down
1 change: 1 addition & 0 deletions packages/integration-sdk-benchmark/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../babel.config');
1 change: 1 addition & 0 deletions packages/integration-sdk-cli/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../babel.config');
2 changes: 1 addition & 1 deletion packages/integration-sdk-cli/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
...require('../../jest.config.base'),
setupFilesAfterEnv: ['./jest.setup.ts'],
setupFilesAfterEnv: ['jest-extended/all', './jest.setup.ts'],
};
Loading

0 comments on commit ccf0f4f

Please sign in to comment.