Skip to content

Commit

Permalink
feat(testing): add --js option for jest generators
Browse files Browse the repository at this point in the history
  • Loading branch information
barbados-clemens committed Apr 22, 2022
1 parent 9261828 commit 05ae2cc
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 12 deletions.
10 changes: 10 additions & 0 deletions docs/generated/packages/jest.json
Expand Up @@ -26,6 +26,11 @@
"type": "boolean",
"default": false,
"description": "Do not add dependencies to `package.json`."
},
"js": {
"type": "boolean",
"default": false,
"description": "Use JavaScript instead of TypeScript for config files"
}
},
"required": [],
Expand Down Expand Up @@ -103,6 +108,11 @@
"type": "boolean",
"default": false,
"description": "Do not add dependencies to `package.json`."
},
"js": {
"type": "boolean",
"default": false,
"description": "Use JavaScript instead of TypeScript for config files"
}
},
"required": [],
Expand Down
5 changes: 5 additions & 0 deletions packages/jest/src/generators/init/init.spec.ts
Expand Up @@ -38,6 +38,11 @@ describe('jest', () => {
expect(packageJson.devDependencies['ts-node']).toBeDefined();
});

it('should make js jest files', () => {
jestInitGenerator(tree, { js: true });
expect(tree.exists('jest.config.js')).toBeTruthy();
expect(tree.exists('jest.preset.js')).toBeTruthy();
});
describe('Deprecated: --babelJest', () => {
it('should add babel dependencies', async () => {
jestInitGenerator(tree, { babelJest: true });
Expand Down
18 changes: 11 additions & 7 deletions packages/jest/src/generators/init/init.ts
Expand Up @@ -23,12 +23,13 @@ interface NormalizedSchema extends ReturnType<typeof normalizeOptions> {}

const schemaDefaults = {
compiler: 'tsc',
js: false,
} as const;

function createJestConfig(host: Tree) {
if (!host.exists('jest.config.ts')) {
function createJestConfig(host: Tree, js: boolean = false) {
if (!host.exists(`jest.config.${js ? 'js' : 'ts'}`)) {
host.write(
'jest.config.ts',
`jest.config.${js ? 'js' : 'ts'}`,
stripIndents`
const { getJestProjects } = require('@nrwl/jest');
Expand All @@ -38,9 +39,9 @@ function createJestConfig(host: Tree) {
);
}

if (!host.exists('jest.preset.ts')) {
if (!host.exists(`jest.preset.${js ? 'js' : 'ts'}`)) {
host.write(
'jest.preset.ts',
`jest.preset.${js ? 'js' : 'ts'}`,
`
const nxPreset = require('@nrwl/jest/preset');
Expand All @@ -62,9 +63,12 @@ function updateDependencies(tree: Tree, options: NormalizedSchema) {
// jest will throw an error if it's not installed
// even if not using it in overriding transformers
'ts-jest': tsJestVersion,
'ts-node': tsNodeVersion,
};

if (!options.js) {
devDeps['ts-node'] = tsNodeVersion;
}

if (options.compiler === 'babel' || options.babelJest) {
devDeps['babel-jest'] = babelJestVersion;
// in some cases @nrwl/web will not already be present i.e. node only projects
Expand Down Expand Up @@ -93,7 +97,7 @@ function updateExtensions(host: Tree) {

export function jestInitGenerator(tree: Tree, schema: JestInitSchema) {
const options = normalizeOptions(schema);
createJestConfig(tree);
createJestConfig(tree, options.js);

let installTask: GeneratorCallback = () => {};
if (!options.skipPackageJson) {
Expand Down
1 change: 1 addition & 0 deletions packages/jest/src/generators/init/schema.d.ts
@@ -1,5 +1,6 @@
export interface JestInitSchema {
compiler?: 'tsc' | 'babel' | 'swc';
js?: boolean;
skipPackageJson?: boolean;
/**
* @deprecated
Expand Down
5 changes: 5 additions & 0 deletions packages/jest/src/generators/init/schema.json
Expand Up @@ -16,6 +16,11 @@
"type": "boolean",
"default": false,
"description": "Do not add dependencies to `package.json`."
},
"js": {
"type": "boolean",
"default": false,
"description": "Use JavaScript instead of TypeScript for config files"
}
},
"required": []
Expand Down
@@ -1,6 +1,6 @@
module.exports = {
displayName: '<%= project %>',
preset: '<%= offsetFromRoot %>jest.preset.ts',<% if(setupFile !== 'none') { %>
preset: '<%= offsetFromRoot %>jest.preset<%= ext %>',<% if(setupFile !== 'none') { %>
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],<% } %><% if (transformer === 'ts-jest') { %>
globals: {
'ts-jest': {
Expand Down
14 changes: 14 additions & 0 deletions packages/jest/src/generators/jest-project/jest-project.spec.ts
Expand Up @@ -258,6 +258,20 @@ describe('jestProject', () => {
});
});

it('should create jest.config.js with --js flag', async () => {
await jestProjectGenerator(tree, {
...defaultOptions,
project: 'lib1',
js: true,
} as JestProjectSchema);
expect(tree.exists('jest.preset.js')).toBeTruthy();
expect(tree.exists('jest.config.js')).toBeTruthy();
expect(tree.exists('libs/lib1/jest.config.js')).toBeTruthy();
expect(tree.read('libs/lib1/jest.config.js', 'utf-8')).toContain(
"preset: '../../jest.preset.js',"
);
});

describe('--babelJest', () => {
it('should have globals.ts-jest configured when babelJest is false', async () => {
await jestProjectGenerator(tree, {
Expand Down
8 changes: 8 additions & 0 deletions packages/jest/src/generators/jest-project/lib/create-files.ts
Expand Up @@ -26,6 +26,7 @@ export function createFiles(tree: Tree, options: JestProjectSchema) {
tmpl: '',
...options,
transformer,
ext: options.js && tree.exists('jest.preset.js') ? '.js' : '.ts',
projectRoot: projectConfig.root,
offsetFromRoot: offsetFromRoot(projectConfig.root),
});
Expand All @@ -42,4 +43,11 @@ export function createFiles(tree: Tree, options: JestProjectSchema) {
})
);
}

if (options.js) {
tree.rename(
join(projectConfig.root, 'jest.config.ts'),
join(projectConfig.root, 'jest.config.js')
);
}
}
Expand Up @@ -2,18 +2,21 @@ import { JestProjectSchema } from '../schema';
import { addPropertyToJestConfig } from '../../../utils/config/update-config';
import { readProjectConfiguration, Tree } from '@nrwl/devkit';

function isUsingUtilityFunction(host: Tree) {
return host.read('jest.config.ts').toString().includes('getJestProjects()');
function isUsingUtilityFunction(host: Tree, js = false) {
return host
.read(`jest.config.${js ? 'js' : 'ts'}`)
.toString()
.includes('getJestProjects()');
}

export function updateJestConfig(host: Tree, options: JestProjectSchema) {
if (isUsingUtilityFunction(host)) {
if (isUsingUtilityFunction(host, options.js)) {
return;
}
const project = readProjectConfiguration(host, options.project);
addPropertyToJestConfig(
host,
'jest.config.ts',
`jest.config.${options.js ? 'js' : 'ts'}`,
'projects',
`<rootDir>/${project.root}`
);
Expand Down
1 change: 1 addition & 0 deletions packages/jest/src/generators/jest-project/schema.d.ts
Expand Up @@ -15,4 +15,5 @@ export interface JestProjectSchema {
skipFormat?: boolean;
compiler?: 'tsc' | 'babel' | 'swc';
skipPackageJson?: boolean;
js?: boolean;
}
5 changes: 5 additions & 0 deletions packages/jest/src/generators/jest-project/schema.json
Expand Up @@ -63,6 +63,11 @@
"type": "boolean",
"default": false,
"description": "Do not add dependencies to `package.json`."
},
"js": {
"type": "boolean",
"default": false,
"description": "Use JavaScript instead of TypeScript for config files"
}
},
"required": []
Expand Down

0 comments on commit 05ae2cc

Please sign in to comment.