@nx/next
Version:
67 lines (60 loc) • 2.06 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateJestConfig = updateJestConfig;
const devkit_1 = require("@nx/devkit");
function findProjectJestConfig(host, projectRoot) {
const extensions = ['js', 'ts', 'cts'];
for (const ext of extensions) {
const configPath = `${projectRoot}/jest.config.${ext}`;
if (host.exists(configPath)) {
return configPath;
}
}
return null;
}
function updateJestConfig(host, options) {
if (options.unitTestRunner !== 'jest') {
return;
}
const configPath = findProjectJestConfig(host, options.projectRoot);
if (!configPath)
return;
const offset = (0, devkit_1.offsetFromRoot)(options.projectRoot);
const isCommonJS = configPath.endsWith('.js') || configPath.endsWith('.cts');
// Easier to override the whole file rather than replace content since the structure has changed with `next/jest.js` being used.
const newContent = isCommonJS
? `const nextJest = require('next/jest.js');
const createJestConfig = nextJest({
dir: './',
});
const config = {
displayName: '${options.projectName}',
preset: '${offset}jest.preset.js',
transform: {
'^(?!.*\\\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '${offset}coverage/${options.projectRoot}',
testEnvironment: 'jsdom',
};
module.exports = createJestConfig(config);
`
: `import type { Config } from 'jest';
import nextJest from 'next/jest.js';
const createJestConfig = nextJest({
dir: './',
});
const config: Config = {
displayName: '${options.projectName}',
preset: '${offset}jest.preset.js',
transform: {
'^(?!.*\\\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '${offset}coverage/${options.projectRoot}',
testEnvironment: 'jsdom',
};
export default createJestConfig(config);
`;
host.write(configPath, newContent);
}