Skip to content

Commit

Permalink
restrict usage of Jest globals in the codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
akphi committed Jun 1, 2022
1 parent f3add7b commit af1597a
Show file tree
Hide file tree
Showing 74 changed files with 310 additions and 131 deletions.
21 changes: 21 additions & 0 deletions .changeset/chatty-pens-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
'@finos/legend-application': patch
'@finos/legend-art': patch
'@finos/legend-dev-utils': patch
'@finos/legend-extension-dsl-data-space': patch
'@finos/legend-extension-dsl-diagram': patch
'@finos/legend-extension-dsl-persistence': patch
'@finos/legend-extension-dsl-text': patch
'@finos/legend-extension-external-store-service': patch
'@finos/legend-graph': patch
'@finos/legend-manual-tests': patch
'@finos/legend-query': patch
'@finos/legend-query-deployment': patch
'@finos/legend-server-depot': patch
'@finos/legend-server-sdlc': patch
'@finos/legend-shared': patch
'@finos/legend-studio': patch
'@finos/legend-studio-deployment': patch
'@finos/legend-studio-extension-query-builder': patch
'@finos/legend-taxonomy-deployment': patch
---
6 changes: 6 additions & 0 deletions .changeset/eight-colts-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@finos/legend-shared': major
'@finos/legend-dev-utils': major
---

**BREAKING CHANGE:** Use `@jest/globals` to import `jest` constructs, such as, `expect`, `test`, etc. We bumped into some problem when trying to disable `injectGlobals` in `Jest` config, so that would be left on as default for now, but at least with this change, we restrict usage of `jest` globals in the codebase.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
"@finos/eslint-plugin-legend-studio": "workspace:*",
"@finos/legend-dev-utils": "workspace:*",
"@finos/stylelint-config-legend-studio": "workspace:*",
"@types/jest": "27.5.1",
"@types/node": "17.0.38",
"chalk": "5.0.1",
"cross-env": "7.0.3",
Expand All @@ -97,7 +96,7 @@
"husky": "8.0.1",
"inquirer": "8.2.4",
"jest": "28.1.0",
"lint-staged": "12.5.0",
"lint-staged": "13.0.0",
"micromatch": "4.0.5",
"npm-run-all": "4.1.5",
"prettier": "2.6.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { jest } from '@jest/globals';
import { createMemoryHistory } from 'history';
import { ApplicationStore } from '../stores/ApplicationStore';
import { WebApplicationNavigator } from '../stores/WebApplicationNavigator';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { jest } from '@jest/globals';
import { WebApplicationNavigator } from '../stores/WebApplicationNavigator';
import { createMemoryHistory, type History } from 'history';

Expand Down
2 changes: 2 additions & 0 deletions packages/legend-art/src/testMocks/MonacoEditorMockUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import { jest } from '@jest/globals';

/**
* NOTE: we have tried different ways to mock `monaco-editor`. But those ways often involve
* trying to load `monaco-editor` from `node_modules` and that takes a long time, so we'd better just mock
Expand Down
5 changes: 5 additions & 0 deletions packages/legend-dev-utils/JestConfigUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export const getBaseConfig = ({
// the built-in GitHub Actions Reporter will annotate changed files with test failure messages
'github-actions',
],
// NOTE: we cannot really turn this flag on, though we have tried to disabled usage of global jest
// within the code base, some libraries we rely on still need global injections apparently,
// we observed problems with mock libraries like `jest-canvas-mock`, timing issues, and duplicated
// rendering of elements in general
// injectGlobals: false,
});

export const unitTest = (testName) => `[UNIT] ${testName}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { test, expect } from '@jest/globals';
import { resolve } from 'path';
import {
getTsConfigJSON,
Expand Down
1 change: 1 addition & 0 deletions packages/legend-dev-utils/jest/setupJestExpectExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { expect } from '@jest/globals';
import {
toContainKeys,
toContainAllKeys,
Expand Down
2 changes: 2 additions & 0 deletions packages/legend-dev-utils/jest/setupTestEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import { jest } from '@jest/globals';

// Increase timeout for some long running tests
jest.setTimeout(10000);
// Runs failed tests n-times until they pass or until the max number of retries is exhausted.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { test } from '@jest/globals';
import type { Entity } from '@finos/legend-model-storage';
import { unitTest } from '@finos/legend-shared';
import { TEST_DATA__roundtrip } from './TEST_DATA__DSLDataSpace_Roundtrip';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { test } from '@jest/globals';
import { waitFor } from '@testing-library/react';
import { integrationTest } from '@finos/legend-shared';
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { test, expect } from '@jest/globals';
import { unitTest } from '@finos/legend-shared';
import type { Entity } from '@finos/legend-model-storage';
import { DSLDiagram_GraphPreset } from '../../DSLDiagram_Extension';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { test } from '@jest/globals';
import type { Entity } from '@finos/legend-model-storage';
import { unitTest } from '@finos/legend-shared';
import { TEST_DATA__roundtrip } from './TEST_DATA__DSLDiagram_Roundtrip';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { test } from '@jest/globals';
import { DSLPersistence_GraphPreset } from '../../DSLPersistence_Extension';
import { TEST_DATA__roundtrip } from './TEST_DATA__DSLPersistence_Roundtrip';
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { test } from '@jest/globals';
import type { Entity } from '@finos/legend-model-storage';
import { unitTest } from '@finos/legend-shared';
import { TEST_DATA__roundtrip } from './TEST_DATA__DSLText_Roundtrip';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { test } from '@jest/globals';
import type { Entity } from '@finos/legend-model-storage';
import { unitTest } from '@finos/legend-shared';
import { roundtripTestData } from './TEST_DATA__ESService_Roundtrip';
Expand Down
17 changes: 10 additions & 7 deletions packages/legend-graph/src/GraphManagerTestUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
* limitations under the License.
*/

/// <reference types="jest-extended" />
import { jest, expect } from '@jest/globals';
import {
type LoggerPlugin,
type TEMPORARRY__JestMatcher,
Log,
AbstractPluginManager,
promisify,
Expand Down Expand Up @@ -194,10 +195,12 @@ export const TEST__checkGraphHashUnchanged = async (
}),
),
);
expect(
Array.from(originalHashesIndex.entries()).filter(
(entry) => entry[0] !== SECTION_INDEX_ELEMENT_PATH,
),
(
expect(
Array.from(originalHashesIndex.entries()).filter(
(entry) => entry[0] !== SECTION_INDEX_ELEMENT_PATH,
),
) as TEMPORARRY__JestMatcher
).toIncludeSameMembers(
Array.from(graphHashesIndex.entries()).filter(
(entry) => entry[0] !== SECTION_INDEX_ELEMENT_PATH,
Expand All @@ -222,7 +225,7 @@ export const TEST__checkBuildingElementsRoundtrip = async (
TEST__ensureObjectFieldsAreSortedAlphabetically(entity.content),
);
// check if the contents are the same (i.e. roundtrip test)
expect(transformedEntities).toIncludeSameMembers(
(expect(transformedEntities) as TEMPORARRY__JestMatcher).toIncludeSameMembers(
TEST__excludeSectionIndex(entities),
);
await TEST__checkGraphHashUnchanged(graphManagerState, entities);
Expand All @@ -247,7 +250,7 @@ export const TEST__checkBuildingResolvedElements = async (
TEST__ensureObjectFieldsAreSortedAlphabetically(entity.content),
);
// check if the contents are the same (i.e. roundtrip test)
expect(transformedEntities).toIncludeSameMembers(
(expect(transformedEntities) as TEMPORARRY__JestMatcher).toIncludeSameMembers(
TEST__excludeSectionIndex(resolvedEntities),
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { test, describe, expect } from '@jest/globals';
import type { Entity } from '@finos/legend-model-storage';
import { unitTest } from '@finos/legend-shared';
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { test, describe } from '@jest/globals';
import { unitTest } from '@finos/legend-shared';
import {
TEST_DATA__AutoImportsWithSystemProfiles,
Expand Down
32 changes: 18 additions & 14 deletions packages/legend-graph/src/__tests__/Inference.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

/// <reference types="jest-extended" />
import { unitTest } from '@finos/legend-shared';
import { test, expect } from '@jest/globals';
import { type TEMPORARRY__JestMatcher, unitTest } from '@finos/legend-shared';
import {
TEST_DATA__InferenceDefaultMappingElementID,
TEST_DATA__ImportResolutionMultipleMatchesFound,
Expand All @@ -39,7 +39,7 @@ test(unitTest('Infer default mapping element ID'), async () => {
const transformedEntities = graphManagerState.graph.allOwnElements.map(
(element) => graphManagerState.graphManager.elementToEntity(element),
);
expect(transformedEntities).toIncludeSameMembers(
(expect(transformedEntities) as TEMPORARRY__JestMatcher).toIncludeSameMembers(
TEST__excludeSectionIndex(
TEST_DATA__InferenceDefaultMappingElementID as Entity[],
),
Expand Down Expand Up @@ -76,9 +76,9 @@ test(
const transformedEntities = graphManagerState.graph.allOwnElements.map(
(element) => graphManagerState.graphManager.elementToEntity(element),
);
expect(transformedEntities).toIncludeSameMembers(
TEST_DATA__ReferenceWithoutSection.withoutSection,
);
(
expect(transformedEntities) as TEMPORARRY__JestMatcher
).toIncludeSameMembers(TEST_DATA__ReferenceWithoutSection.withoutSection);
},
);

Expand All @@ -103,10 +103,12 @@ test(
graphManagerState.graph.getProfile('test::tProf');
}

expect(
graphManagerState.graph.allOwnElements.map((element) =>
graphManagerState.graphManager.elementToEntity(element),
),
(
expect(
graphManagerState.graph.allOwnElements.map((element) =>
graphManagerState.graphManager.elementToEntity(element),
),
) as TEMPORARRY__JestMatcher
).toIncludeSameMembers(
TEST__excludeSectionIndex(
TEST_DATA__ReferenceModification.sameProfileModification as Entity[],
Expand All @@ -130,10 +132,12 @@ test(
graphManagerState.graph.getProfile('test2::tProf');
}

expect(
graphManagerState.graph.allOwnElements.map((element) =>
graphManagerState.graphManager.elementToEntity(element),
),
(
expect(
graphManagerState.graph.allOwnElements.map((element) =>
graphManagerState.graphManager.elementToEntity(element),
),
) as TEMPORARRY__JestMatcher
).toIncludeSameMembers(
TEST__excludeSectionIndex(
TEST_DATA__ReferenceModification.differentProfileModification as Entity[],
Expand Down
1 change: 1 addition & 0 deletions packages/legend-graph/src/__tests__/MetaModelUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { test, expect } from '@jest/globals';
import {
extractElementNameFromPath,
fromElementPathToMappingElementId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { test, expect } from '@jest/globals';
import { unitTest } from '@finos/legend-shared';
import { addElementToPackage } from '../helpers/DomainHelper';
import { Class } from '../models/metamodels/pure/packageableElements/domain/Class';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { test } from '@jest/globals';
import type { Entity } from '@finos/legend-model-storage';
import { unitTest } from '@finos/legend-shared';
import { roundtripTestData } from './TEST_DATA__DSLExternalFormat_Roundtrip';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

import { unitTest } from '@finos/legend-shared';
import { test, expect } from '@jest/globals';
import { type TEMPORARRY__JestMatcher, unitTest } from '@finos/legend-shared';
import { TEST__getTestGraphManagerState } from '../../GraphManagerTestUtils';
import { DependencyManager } from '../../graph/DependencyManager';
import type { Entity } from '@finos/legend-model-storage';
Expand Down Expand Up @@ -128,7 +129,9 @@ test(
const transformedEntities = graphManagerState.graph.allOwnElements.map(
(el) => graphManagerState.graphManager.elementToEntity(el),
);
expect(entities).toIncludeSameMembers(transformedEntities);
(expect(entities) as TEMPORARRY__JestMatcher).toIncludeSameMembers(
transformedEntities,
);
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
* limitations under the License.
*/

/// <reference types="jest-extended" />
import { unitTest, guaranteeNonNullable } from '@finos/legend-shared';
import { test, expect } from '@jest/globals';
import {
type TEMPORARRY__JestMatcher,
unitTest,
guaranteeNonNullable,
} from '@finos/legend-shared';
import {
TEST_DATA__simpleDebuggingCase,
TEST_DATA__AutoImportsWithAny,
Expand Down Expand Up @@ -93,7 +97,9 @@ const testGeneratedElements = async (
const transformedEntities = graphManagerState.graph.allOwnElements.map((el) =>
graphManagerState.graphManager.elementToEntity(el),
);
expect(entities).toIncludeSameMembers(transformedEntities);
(expect(entities) as TEMPORARRY__JestMatcher).toIncludeSameMembers(
transformedEntities,
);
// Ensure generated elements are not transformed
for (const entityPath of generatedElementPaths) {
expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { test, expect, beforeEach } from '@jest/globals';
import {
TEST_DATA__MissingSuperType,
TEST_DATA__MissingProfile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { test, expect, beforeAll } from '@jest/globals';
import TEST_DATA__m2mGraphEntities from './TEST_DATA__M2MGraphEntities.json';
import { guaranteeNonNullable, unitTest } from '@finos/legend-shared';
import type { Entity } from '@finos/legend-model-storage';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { test } from '@jest/globals';
import type { Entity } from '@finos/legend-model-storage';
import { unitTest } from '@finos/legend-shared';
import { TEST__checkBuildingResolvedElements } from '../../GraphManagerTestUtils';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { test, expect, beforeEach } from '@jest/globals';
import TEST_DATA__fileGeneration from './TEST_DATA__FileGeneration.json';
import { guaranteeType, unitTest } from '@finos/legend-shared';
import type { Entity } from '@finos/legend-model-storage';
Expand Down
Loading

0 comments on commit af1597a

Please sign in to comment.