fix(jest): warn with a helpful warning when unable to update the config

This commit is contained in:
Jason Jean 2020-12-17 13:15:40 -05:00
parent e63c3d162b
commit fcfc8589d5
3 changed files with 74 additions and 49 deletions

View File

@ -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;
}

View File

@ -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(() => {
describe('warnings', () => {
beforeEach(() => {
spyOn(console, 'warn').and.callThrough();
});
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'
);
}).toThrow();
expect(console.warn).toHaveBeenCalled();
});
it('should throw an error if the jest.config doesnt match module.exports = {} style', () => {
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();
expect(console.warn).toHaveBeenCalled();
});
it('should throw if the provided config does not exist in the tree', () => {

View File

@ -19,6 +19,10 @@ export function addPropertyToJestConfig(
propertyName: string,
value: unknown
) {
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(
@ -28,6 +32,14 @@ export function addPropertyToJestConfig(
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,6 +53,10 @@ export function removePropertyFromJestConfig(
path: string,
propertyName: string
) {
if (!host.exists(path)) {
throw new Error(`Cannot find '${path}' in your workspace.`);
}
try {
const configObject = jestConfigObjectAst(host, path);
const propertyAssignment = removeProperty(
configObject,
@ -58,4 +74,10 @@ export function removePropertyFromJestConfig(
),
]);
}
} catch (e) {
console.warn(
`Could not automatically remove the '${propertyName}' property from ${path}:`
);
console.log(`Please manually update ${path}`);
}
}