Skip to content

Commit

Permalink
fix(testing): add eslint ignore comments for jest config properties
Browse files Browse the repository at this point in the history
fixes: #10021
  • Loading branch information
barbados-clemens committed May 4, 2022
1 parent 1ea5277 commit 432da8e
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`jestProject --babelJest should generate proper jest.transform when --compiler=swc and supportTsx is true 1`] = `
"module.exports = {
"/* eslint-disable */
export default {
displayName: 'lib1',
preset: '../../jest.preset.js',
transform: {
Expand All @@ -14,7 +15,8 @@ exports[`jestProject --babelJest should generate proper jest.transform when --co
`;

exports[`jestProject --babelJest should generate proper jest.transform when babelJest and supportTsx is true 1`] = `
"module.exports = {
"/* eslint-disable */
export default {
displayName: 'lib1',
preset: '../../jest.preset.js',
transform: {
Expand All @@ -27,7 +29,8 @@ exports[`jestProject --babelJest should generate proper jest.transform when babe
`;

exports[`jestProject --babelJest should generate proper jest.transform when babelJest is true 1`] = `
"module.exports = {
"/* eslint-disable */
export default {
displayName: 'lib1',
preset: '../../jest.preset.js',
transform: {
Expand All @@ -40,7 +43,8 @@ exports[`jestProject --babelJest should generate proper jest.transform when babe
`;

exports[`jestProject --setup-file should have setupFilesAfterEnv and globals.ts-jest in the jest.config when generated for angular 1`] = `
"module.exports = {
"/* eslint-disable */
export default {
displayName: 'lib1',
preset: '../../jest.preset.js',
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
Expand All @@ -65,7 +69,8 @@ exports[`jestProject --setup-file should have setupFilesAfterEnv and globals.ts-
`;

exports[`jestProject should create a jest.config.ts 1`] = `
"module.exports = {
"/* eslint-disable */
export default {
displayName: 'lib1',
preset: '../../jest.preset.js',
globals: {
Expand All @@ -79,7 +84,8 @@ exports[`jestProject should create a jest.config.ts 1`] = `
`;

exports[`jestProject should generate files 1`] = `
"module.exports = {
"/* eslint-disable */
export default {
displayName: 'lib1',
preset: '../../jest.preset.js',
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<% if(ext === 'ts') {%>export default {<% } else { %>module.exports = {<% } %>
/* eslint-disable */
<% if(js){ %>module.exports =<% } else{ %>export default<% } %> {
displayName: '<%= project %>',
preset: '<%= offsetFromRoot %>jest.preset.js',
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<% if(ext === 'ts') {%>export default {<% } else { %>module.exports = {<% } %>
/* eslint-disable */
<% if(js){ %>module.exports =<% } else{ %>export default<% } %> {
displayName: '<%= project %>',
preset: '<%= offsetFromRoot %>jest.preset.js',<% if(setupFile !== 'none') { %>
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],<% } %><% if (transformer === 'ts-jest') { %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function createFiles(tree: Tree, options: JestProjectSchema) {
tmpl: '',
...options,
transformer,
ext: options.js, // it doesn't matter if the root jest.config is ts or js
js: options.js,
projectRoot: projectConfig.root,
offsetFromRoot: offsetFromRoot(projectConfig.root),
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Jest Migration (v14.0.0) should NOT update jest.config.ts preset 1`] = `
exports[`Jest Migration (v14.0.0) should rename project jest.config.js to jest.config.ts 1`] = `
"module.exports = {
displayName: 'lib-one',
preset: '../../jest.preset.js',
Expand All @@ -18,7 +18,7 @@ exports[`Jest Migration (v14.0.0) should NOT update jest.config.ts preset 1`] =
"
`;

exports[`Jest Migration (v14.0.0) should rename project jest.config.js to jest.config.ts 1`] = `
exports[`Jest Migration (v14.0.0) should update jest.config.ts preset to use the jest.preset.ts 1`] = `
"module.exports = {
displayName: 'lib-one',
preset: '../../jest.preset.js',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ function updateTsConfig(tree: Tree, tsConfigPath: string) {
}
}

function addEsLintIgnoreComments(tree: Tree, filePath: string) {
if (tree.exists(filePath)) {
const contents = tree.read(filePath, 'utf-8');
tree.write(
filePath,
`/* eslint-disable */
${contents}`
);
}
}

function isJestConfigValid(tree: Tree, options: JestExecutorOptions) {
const configExt = extname(options.jestConfig);

Expand Down Expand Up @@ -73,6 +84,8 @@ export async function updateJestConfigExt(tree: Tree) {
return;
}

addEsLintIgnoreComments(tree, options.jestConfig);

const newJestConfigPath = options.jestConfig.replace('.js', '.ts');
tree.rename(options.jestConfig, newJestConfigPath);

Expand Down
26 changes: 15 additions & 11 deletions packages/jest/src/utils/config/functions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as ts from 'typescript';
import { applyChangesToString, ChangeType, Tree } from '@nrwl/devkit';
import { Config } from '@jest/types';
import { createContext, runInContext } from 'vm';
import { dirname, join } from 'path';
import {applyChangesToString, ChangeType, Tree} from '@nrwl/devkit';
import {Config} from '@jest/types';
import {createContext, runInContext} from 'vm';
import {dirname, join} from 'path';

function makeTextToInsert(
value: unknown,
Expand Down Expand Up @@ -93,7 +93,7 @@ export function addOrUpdateProperty(
const text = makeTextToInsert(
value,
arrayLiteral.elements.length !== 0 &&
!arrayLiteral.elements.hasTrailingComma
!arrayLiteral.elements.hasTrailingComma
);
const updatedContents = applyChangesToString(originalContents, [
{
Expand Down Expand Up @@ -150,7 +150,7 @@ export function removeProperty(
if (
properties.length > 0 &&
propertyAssignment.initializer.kind ===
ts.SyntaxKind.ObjectLiteralExpression
ts.SyntaxKind.ObjectLiteralExpression
) {
return removeProperty(
propertyAssignment.initializer as ts.ObjectLiteralExpression,
Expand Down Expand Up @@ -253,16 +253,20 @@ export function jestConfigObject(
): Partial<Config.InitialOptions> & { [index: string]: any } {
const __filename = join(host.root, path);
const contents = host.read(path, 'utf-8');
let module = { exports: {} };
let module = {exports: {}};

// TODO(caleb): handle imports
// transform the export default syntax to module.exports
// const forcedModuleSyntax = `module.exports = ${jestConfigObjectAst(contents).getText()}`;

// this will work for the default config, but will break if there are any other ts syntax
// TODO(caleb): use the AST to transform back to the module.exports syntax so this will keep working
// or deprecate and make a new method for getting the jest config object
const forcedModuleSyntax = contents.replace(
/export\s+default/,
'module.exports ='
);
// Run the contents of the file with some stuff from this current context
// The module.exports will be mutated by the contents of the file...
runInContext(
contents,
forcedModuleSyntax,
createContext({
module,
require,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable */
const fs = require('fs');

// Reading the SWC compilation config and remove the "exclude"
Expand Down
3 changes: 2 additions & 1 deletion packages/js/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,8 @@ describe('lib', () => {
expect(tree.exists(`libs/my-lib/jest.config.ts`)).toBeTruthy();
expect(tree.read(`libs/my-lib/jest.config.ts`, 'utf-8'))
.toMatchInlineSnapshot(`
"module.exports = {
"/* eslint-disable */
module.exports = {
displayName: 'my-lib',
preset: '../../jest.preset.js',
globals: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ exports[`@nrwl/linter:workspace-rules-project should generate the required files
`;

exports[`@nrwl/linter:workspace-rules-project should generate the required files 5`] = `
"module.exports = {
"/* eslint-disable */
module.exports = {
displayName: 'eslint-rules',
preset: '../../jest.preset.js',
globals: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`lib --testEnvironment should set target jest testEnvironment to jsdom 1`] = `
"module.exports = {
"/* eslint-disable */
module.exports = {
displayName: 'my-lib',
preset: '../../jest.preset.js',
globals: {
Expand All @@ -19,7 +20,8 @@ exports[`lib --testEnvironment should set target jest testEnvironment to jsdom 1
`;

exports[`lib --testEnvironment should set target jest testEnvironment to node by default 1`] = `
"module.exports = {
"/* eslint-disable */
module.exports = {
displayName: 'my-lib',
preset: '../../jest.preset.js',
globals: {
Expand Down
3 changes: 2 additions & 1 deletion packages/node/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ describe('app', () => {

expect(tree.read(`apps/my-node-app/jest.config.ts`, 'utf-8'))
.toMatchInlineSnapshot(`
"module.exports = {
"/* eslint-disable */
module.exports = {
displayName: 'my-node-app',
preset: '../../jest.preset.js',
testEnvironment: 'node',
Expand Down
3 changes: 2 additions & 1 deletion packages/node/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,8 @@ describe('lib', () => {

expect(tree.read(`libs/my-lib/jest.config.ts`, 'utf-8'))
.toMatchInlineSnapshot(`
"module.exports = {
"/* eslint-disable */
module.exports = {
displayName: 'my-lib',
preset: '../../jest.preset.js',
testEnvironment: 'node',
Expand Down
6 changes: 4 additions & 2 deletions packages/web/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ describe('app', () => {

expect(tree.read(`apps/my-app/jest.config.ts`, 'utf-8'))
.toMatchInlineSnapshot(`
"module.exports = {
"/* eslint-disable */
module.exports = {
displayName: 'my-app',
preset: '../../jest.preset.js',
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
Expand All @@ -443,7 +444,8 @@ describe('app', () => {

expect(tree.read(`apps/my-app/jest.config.ts`, 'utf-8'))
.toMatchInlineSnapshot(`
"module.exports = {
"/* eslint-disable */
module.exports = {
displayName: 'my-app',
preset: '../../jest.preset.js',
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
Expand Down
6 changes: 4 additions & 2 deletions packages/workspace/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ describe('lib', () => {
expect(tree.exists(`libs/my-lib/jest.config.ts`)).toBeTruthy();
expect(tree.read(`libs/my-lib/jest.config.ts`, 'utf-8'))
.toMatchInlineSnapshot(`
"module.exports = {
"/* eslint-disable */
module.exports = {
displayName: 'my-lib',
preset: '../../jest.preset.js',
globals: {
Expand Down Expand Up @@ -829,7 +830,8 @@ describe('lib', () => {

expect(tree.read(`libs/my-lib/jest.config.ts`, 'utf-8'))
.toMatchInlineSnapshot(`
"module.exports = {
"/* eslint-disable */
module.exports = {
displayName: 'my-lib',
preset: '../../jest.preset.js',
transform: {
Expand Down

0 comments on commit 432da8e

Please sign in to comment.