fix(jest): warn with a helpful warning when unable to update the config
This commit is contained in:
parent
e63c3d162b
commit
fcfc8589d5
@ -160,10 +160,6 @@ export function jestConfigObjectAst(
|
||||
host: Tree,
|
||||
path: string
|
||||
): ts.ObjectLiteralExpression {
|
||||
if (!host.exists(path)) {
|
||||
throw new Error(`Cannot find '${path}' in your workspace.`);
|
||||
}
|
||||
|
||||
const fileContent = host.read(path).toString('utf-8');
|
||||
|
||||
const sourceFile = ts.createSourceFile(
|
||||
@ -181,8 +177,7 @@ export function jestConfigObjectAst(
|
||||
const moduleExports = expressions.find(
|
||||
(node) =>
|
||||
node.left.getText() === 'module.exports' &&
|
||||
node.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
|
||||
ts.isObjectLiteralExpression(node.right)
|
||||
node.operatorToken.kind === ts.SyntaxKind.EqualsToken
|
||||
);
|
||||
|
||||
if (!moduleExports) {
|
||||
@ -193,6 +188,12 @@ export function jestConfigObjectAst(
|
||||
);
|
||||
}
|
||||
|
||||
if (!ts.isObjectLiteralExpression(moduleExports.right)) {
|
||||
throw new Error(
|
||||
`The 'module.exports' expression is not an object literal.`
|
||||
);
|
||||
}
|
||||
|
||||
return moduleExports.right as ts.ObjectLiteralExpression;
|
||||
}
|
||||
|
||||
|
||||
@ -164,19 +164,22 @@ describe('Update jest.config.js', () => {
|
||||
expect(json['update-me']).toEqual('goodbye');
|
||||
});
|
||||
|
||||
describe('errors', () => {
|
||||
it('should throw an error when trying to add a value to an already existing object without being dot delimited', () => {
|
||||
expect(() => {
|
||||
addPropertyToJestConfig(
|
||||
host,
|
||||
'jest.config.js',
|
||||
'alreadyExistingObject',
|
||||
'should fail'
|
||||
);
|
||||
}).toThrow();
|
||||
describe('warnings', () => {
|
||||
beforeEach(() => {
|
||||
spyOn(console, 'warn').and.callThrough();
|
||||
});
|
||||
|
||||
it('should throw an error if the jest.config doesnt match module.exports = {} style', () => {
|
||||
it('should warn when trying to add a value to an already existing object without being dot delimited', () => {
|
||||
addPropertyToJestConfig(
|
||||
host,
|
||||
'jest.config.js',
|
||||
'alreadyExistingObject',
|
||||
'should fail'
|
||||
);
|
||||
expect(console.warn).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should warn if the jest.config doesnt match module.exports = {} style', () => {
|
||||
host.create(
|
||||
'jest.unconventional.js',
|
||||
String.raw`
|
||||
@ -187,14 +190,13 @@ describe('Update jest.config.js', () => {
|
||||
module.exports = jestObject;
|
||||
`
|
||||
);
|
||||
expect(() => {
|
||||
addPropertyToJestConfig(
|
||||
host,
|
||||
'jest.unconventional.js',
|
||||
'stuffhere',
|
||||
'should fail'
|
||||
);
|
||||
}).toThrow();
|
||||
addPropertyToJestConfig(
|
||||
host,
|
||||
'jest.unconventional.js',
|
||||
'stuffhere',
|
||||
'should fail'
|
||||
);
|
||||
expect(console.warn).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should throw if the provided config does not exist in the tree', () => {
|
||||
|
||||
@ -19,15 +19,27 @@ export function addPropertyToJestConfig(
|
||||
propertyName: string,
|
||||
value: unknown
|
||||
) {
|
||||
const configObject = jestConfigObjectAst(host, path);
|
||||
const properties = propertyName.split('.');
|
||||
const changes = addOrUpdateProperty(
|
||||
configObject,
|
||||
properties,
|
||||
JSON.stringify(value),
|
||||
path
|
||||
);
|
||||
insert(host, path, changes);
|
||||
if (!host.exists(path)) {
|
||||
throw new Error(`Cannot find '${path}' in your workspace.`);
|
||||
}
|
||||
try {
|
||||
const configObject = jestConfigObjectAst(host, path);
|
||||
const properties = propertyName.split('.');
|
||||
const changes = addOrUpdateProperty(
|
||||
configObject,
|
||||
properties,
|
||||
JSON.stringify(value),
|
||||
path
|
||||
);
|
||||
insert(host, path, changes);
|
||||
} catch (e) {
|
||||
console.warn(
|
||||
`Could not automatically add the following property to ${path}:`
|
||||
);
|
||||
console.warn(`${propertyName}: ${JSON.stringify(value)}`);
|
||||
console.log(`Please manually update ${path}`);
|
||||
console.warn(`Error: ${e.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -41,21 +53,31 @@ export function removePropertyFromJestConfig(
|
||||
path: string,
|
||||
propertyName: string
|
||||
) {
|
||||
const configObject = jestConfigObjectAst(host, path);
|
||||
const propertyAssignment = removeProperty(
|
||||
configObject,
|
||||
propertyName.split('.')
|
||||
);
|
||||
if (!host.exists(path)) {
|
||||
throw new Error(`Cannot find '${path}' in your workspace.`);
|
||||
}
|
||||
try {
|
||||
const configObject = jestConfigObjectAst(host, path);
|
||||
const propertyAssignment = removeProperty(
|
||||
configObject,
|
||||
propertyName.split('.')
|
||||
);
|
||||
|
||||
if (propertyAssignment) {
|
||||
const file = host.read(path).toString('utf-8');
|
||||
const commaNeeded = file[propertyAssignment.end] === ',';
|
||||
insert(host, path, [
|
||||
new RemoveChange(
|
||||
path,
|
||||
propertyAssignment.getStart(),
|
||||
`${propertyAssignment.getText()}${commaNeeded ? ',' : ''}`
|
||||
),
|
||||
]);
|
||||
if (propertyAssignment) {
|
||||
const file = host.read(path).toString('utf-8');
|
||||
const commaNeeded = file[propertyAssignment.end] === ',';
|
||||
insert(host, path, [
|
||||
new RemoveChange(
|
||||
path,
|
||||
propertyAssignment.getStart(),
|
||||
`${propertyAssignment.getText()}${commaNeeded ? ',' : ''}`
|
||||
),
|
||||
]);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn(
|
||||
`Could not automatically remove the '${propertyName}' property from ${path}:`
|
||||
);
|
||||
console.log(`Please manually update ${path}`);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user