-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
21,938 additions
and
14 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
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
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
189 changes: 189 additions & 0 deletions
189
packages/jest/src/migrations/update-14-4-0/update-configs-jest-28.spec.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,189 @@ | ||
import { readJson } from '@nrwl/devkit'; | ||
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; | ||
import { libraryGenerator as workspaceLib } from '@nrwl/workspace'; | ||
import { | ||
checkDeps, | ||
updateConfigsJest28, | ||
updateJestConfig, | ||
} from './update-configs-jest-28'; | ||
|
||
const mockJestConfig = ` | ||
import { nxPreset } from '@nrwl/jest/preset' | ||
const myGlobals = ['Math', 'Promise']; | ||
export default { | ||
...nxPreset, | ||
displayName: 'test-ng-app', | ||
preset: '../../jest.preset.js', | ||
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'], | ||
extraGlobals: ['Math', 'Something'], | ||
extraGlobals: [], | ||
extraGlobals: myGlobals, | ||
timers: 'fake', | ||
timers: 'modern', | ||
timers: 'legacy', | ||
timers: 'real', | ||
testURL: 'http://localhost', | ||
testURL: "123abc", | ||
testURL: \`BLAH\`, | ||
testEnvironment: 'jsdom', | ||
testRunner: 'jest-jasmine2', | ||
} | ||
`; | ||
describe('Jest Migration - jest 28 config support', () => { | ||
it('should update "extraGlobals" config option', () => { | ||
const actual = updateJestConfig(mockJestConfig); | ||
expect(actual).not.toContain(`extraGlobals`); | ||
expect(actual).toContain(`sandboxInjectedGlobals: ['Math', 'Something'],`); | ||
expect(actual).toContain(`sandboxInjectedGlobals: [],`); | ||
expect(actual).toContain(`sandboxInjectedGlobals: myGlobals,`); | ||
}); | ||
|
||
it('should update "testURL" config option', () => { | ||
const actual = updateJestConfig(mockJestConfig); | ||
expect(actual).not.toContain(`testURL`); | ||
expect(actual).toContain( | ||
`testEnvironmentOptions: {url: 'http://localhost'},` | ||
); | ||
expect(actual).toContain(`testEnvironmentOptions: {url: "123abc"},`); | ||
expect(actual).toContain(`testEnvironmentOptions: {url: \`BLAH\`},`); | ||
}); | ||
|
||
it('should update "timers" config option', () => { | ||
const actual = updateJestConfig(mockJestConfig); | ||
expect(actual).not.toContain(`timers`); | ||
expect(actual).toContain(`fakeTimers: { enableGlobally: false },`); | ||
expect(actual).toContain(`fakeTimers: { enableGlobally: true },`); | ||
expect(actual).toContain(`fakeTimers: { enableGlobally: true },`); | ||
expect(actual).toContain( | ||
`fakeTimers: { enableGlobally: true, legacyFakeTimers: true },` | ||
); | ||
}); | ||
|
||
it('should update jest-environment-jsdom if being used', async () => { | ||
let tree = createTreeWithEmptyWorkspace(2); | ||
tree.write( | ||
`package.json`, | ||
`{ | ||
"name": "jest-28-test", | ||
"version": "0.0.0", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"jest": "^28.1.1", | ||
"jest-environment-jsdom": "^27.1.0", | ||
"jest-preset-angular": "^11.0.0", | ||
"nx": "14.1.6", | ||
"ts-jest": "^27.0.2", | ||
"ts-node": "9.1.1", | ||
"typescript": "~4.6.2" | ||
}, | ||
"dependencies": { | ||
} | ||
} | ||
` | ||
); | ||
|
||
const actual = checkDeps(tree); | ||
expect(actual).toEqual({ | ||
'jest-environment-jsdom': '28.1.1', | ||
}); | ||
}); | ||
|
||
it('should update jest-jasmine2 if being used as a test runner', () => { | ||
let tree = createTreeWithEmptyWorkspace(2); | ||
tree.write( | ||
`package.json`, | ||
`{ | ||
"name": "jest-28-test", | ||
"version": "0.0.0", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"jest": "^27.1.1", | ||
"jest-jasmine2": "^27.1.0", | ||
"nx": "14.1.6", | ||
"ts-jest": "^27.0.2", | ||
"ts-node": "9.1.1", | ||
"typescript": "~4.6.2" | ||
}, | ||
"dependencies": { | ||
} | ||
} | ||
` | ||
); | ||
|
||
const actual = checkDeps(tree); | ||
expect(actual).toEqual({ | ||
'jest-jasmine2': '28.1.1', | ||
}); | ||
}); | ||
|
||
it('should not install deps if they are not used', () => { | ||
let tree = createTreeWithEmptyWorkspace(2); | ||
tree.write( | ||
`package.json`, | ||
`{ | ||
"name": "jest-28-test", | ||
"version": "0.0.0", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"jest": "^27.1.0", | ||
"nx": "14.1.6", | ||
"ts-jest": "^27.0.2", | ||
"ts-node": "9.1.1", | ||
"typescript": "~4.6.2" | ||
}, | ||
"dependencies": { | ||
} | ||
} | ||
` | ||
); | ||
|
||
const actual = checkDeps(tree); | ||
expect(actual).toEqual({}); | ||
}); | ||
|
||
it('should update deps from jest.config.ts', async () => { | ||
let tree = createTreeWithEmptyWorkspace(2); | ||
await workspaceLib(tree, { name: 'my-lib', unitTestRunner: 'jest' }); | ||
tree.write( | ||
'libs/my-lib/jest.config.ts', | ||
` | ||
export default { | ||
displayName: 'test-ng-app', | ||
preset: '../../jest.preset.js', | ||
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'], | ||
globals: { | ||
'ts-jest': { | ||
useESM: true, | ||
tsconfig: '<rootDir>/tsconfig.spec.json', | ||
stringifyContentPathRegex: '\\\\.(html|svg)$', | ||
}, | ||
}, | ||
testEnvironment: 'jsdom', | ||
testRunner: 'jest-jasmine2', | ||
coverageDirectory: '../../coverage/apps/test-ng-app', | ||
transform: { | ||
'^.+\\\\.(ts|mjs|js|html)$': 'jest-preset-angular', | ||
}, | ||
transformIgnorePatterns: [ | ||
'node_modules/(?!.*\\\\.mjs$|rxjs)', | ||
// 'node_modules/(?!rxjs)' | ||
], | ||
snapshotSerializers: [ | ||
'jest-preset-angular/build/serializers/no-ng-attributes', | ||
'jest-preset-angular/build/serializers/ng-snapshot', | ||
'jest-preset-angular/build/serializers/html-comment', | ||
], | ||
}` | ||
); | ||
updateConfigsJest28(tree); | ||
|
||
const packageJson = readJson(tree, 'package.json'); | ||
expect(packageJson.devDependencies).toEqual( | ||
expect.objectContaining({ | ||
'jest-environment-jsdom': '28.1.1', | ||
'jest-jasmine2': '28.1.1', | ||
}) | ||
); | ||
}); | ||
}); |
96 changes: 96 additions & 0 deletions
96
packages/jest/src/migrations/update-14-4-0/update-configs-jest-28.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,96 @@ | ||
import { addDependenciesToPackageJson, readJson, Tree } from '@nrwl/devkit'; | ||
import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils'; | ||
import { JestExecutorOptions } from '../../executors/jest/schema'; | ||
import { | ||
findRootJestConfig, | ||
findRootJestPreset, | ||
} from '../../utils/config/find-root-jest-files'; | ||
import { jestVersion } from '../../utils/versions'; | ||
|
||
const JASMINE_TEST_RUNNER = /(testRunner:\s*['"`])(jest-jasmine2)(['"`])/g; | ||
|
||
const JSDOM_TEST_ENV = /(testEnvironment:\s*['"`])(jsdom)(['"`])/g; | ||
|
||
export function updateConfigsJest28(tree: Tree) { | ||
let devDeps = checkDeps(tree); | ||
forEachExecutorOptions<JestExecutorOptions>( | ||
tree, | ||
'@nrwl/jest:jest', | ||
(options) => { | ||
if (options.jestConfig && tree.exists(options.jestConfig)) { | ||
const updatedConfig = updateJestConfig( | ||
tree.read(options.jestConfig, 'utf-8') | ||
); | ||
tree.write(options.jestConfig, updatedConfig); | ||
|
||
const projectConfigCheck = testFileForDep(updatedConfig); | ||
devDeps = { ...devDeps, ...projectConfigCheck }; | ||
} | ||
} | ||
); | ||
|
||
return addDependenciesToPackageJson(tree, {}, devDeps); | ||
} | ||
|
||
export function updateJestConfig(config: string): string { | ||
return config | ||
.replace( | ||
/(testURL:\s*['"`])(.*)(['"`])/g, | ||
'testEnvironmentOptions: {url: $3$2$3}' | ||
) | ||
.replace(/(extraGlobals)/g, `sandboxInjectedGlobals`) | ||
.replace( | ||
/(timers:\s*['"`])(real)(['"`])/g, | ||
'fakeTimers: { enableGlobally: false }' | ||
) | ||
.replace( | ||
/(timers:\s*['"`])(fake|modern)(['"`])/g, | ||
'fakeTimers: { enableGlobally: true }' | ||
) | ||
.replace( | ||
/(timers:\s*['"`])(legacy)(['"`])/g, | ||
'fakeTimers: { enableGlobally: true, legacyFakeTimers: true }' | ||
); | ||
} | ||
|
||
export function checkDeps(tree: Tree): Record<string, string> { | ||
const packageJson = readJson(tree, 'package.json'); | ||
let devDeps = {}; | ||
|
||
if (packageJson.devDependencies['jest-jasmine2']) { | ||
devDeps['jest-jasmine2'] = jestVersion; | ||
} | ||
if ( | ||
packageJson.devDependencies['jest-environment-jsdom'] || | ||
packageJson.devDependencies['jest-preset-angular'] | ||
) { | ||
devDeps['jest-environment-jsdom'] = jestVersion; | ||
} | ||
|
||
const rootJestConfig = findRootJestConfig(tree); | ||
if (rootJestConfig) { | ||
const rootConfigCheck = testFileForDep(tree.read(rootJestConfig, 'utf-8')); | ||
devDeps = { ...devDeps, ...rootConfigCheck }; | ||
} | ||
|
||
const rootJestPreset = findRootJestPreset(tree); | ||
if (rootJestPreset) { | ||
const rootPresetCheck = testFileForDep(tree.read(rootJestPreset, 'utf-8')); | ||
devDeps = { ...devDeps, ...rootPresetCheck }; | ||
} | ||
|
||
return devDeps; | ||
} | ||
|
||
function testFileForDep(config: string): Record<string, string> { | ||
const deps = {}; | ||
if (JASMINE_TEST_RUNNER.test(config)) { | ||
deps['jest-jasmine2'] = jestVersion; | ||
} | ||
if (JSDOM_TEST_ENV.test(config)) { | ||
deps['jest-environment-jsdom'] = jestVersion; | ||
} | ||
return deps; | ||
} | ||
|
||
export default updateConfigsJest28; |
Oops, something went wrong.