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,
|
host: Tree,
|
||||||
path: string
|
path: string
|
||||||
): ts.ObjectLiteralExpression {
|
): ts.ObjectLiteralExpression {
|
||||||
if (!host.exists(path)) {
|
|
||||||
throw new Error(`Cannot find '${path}' in your workspace.`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const fileContent = host.read(path).toString('utf-8');
|
const fileContent = host.read(path).toString('utf-8');
|
||||||
|
|
||||||
const sourceFile = ts.createSourceFile(
|
const sourceFile = ts.createSourceFile(
|
||||||
@ -181,8 +177,7 @@ export function jestConfigObjectAst(
|
|||||||
const moduleExports = expressions.find(
|
const moduleExports = expressions.find(
|
||||||
(node) =>
|
(node) =>
|
||||||
node.left.getText() === 'module.exports' &&
|
node.left.getText() === 'module.exports' &&
|
||||||
node.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
|
node.operatorToken.kind === ts.SyntaxKind.EqualsToken
|
||||||
ts.isObjectLiteralExpression(node.right)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!moduleExports) {
|
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;
|
return moduleExports.right as ts.ObjectLiteralExpression;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -164,19 +164,22 @@ describe('Update jest.config.js', () => {
|
|||||||
expect(json['update-me']).toEqual('goodbye');
|
expect(json['update-me']).toEqual('goodbye');
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('errors', () => {
|
describe('warnings', () => {
|
||||||
it('should throw an error when trying to add a value to an already existing object without being dot delimited', () => {
|
beforeEach(() => {
|
||||||
expect(() => {
|
spyOn(console, 'warn').and.callThrough();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should warn when trying to add a value to an already existing object without being dot delimited', () => {
|
||||||
addPropertyToJestConfig(
|
addPropertyToJestConfig(
|
||||||
host,
|
host,
|
||||||
'jest.config.js',
|
'jest.config.js',
|
||||||
'alreadyExistingObject',
|
'alreadyExistingObject',
|
||||||
'should fail'
|
'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(
|
host.create(
|
||||||
'jest.unconventional.js',
|
'jest.unconventional.js',
|
||||||
String.raw`
|
String.raw`
|
||||||
@ -187,14 +190,13 @@ describe('Update jest.config.js', () => {
|
|||||||
module.exports = jestObject;
|
module.exports = jestObject;
|
||||||
`
|
`
|
||||||
);
|
);
|
||||||
expect(() => {
|
|
||||||
addPropertyToJestConfig(
|
addPropertyToJestConfig(
|
||||||
host,
|
host,
|
||||||
'jest.unconventional.js',
|
'jest.unconventional.js',
|
||||||
'stuffhere',
|
'stuffhere',
|
||||||
'should fail'
|
'should fail'
|
||||||
);
|
);
|
||||||
}).toThrow();
|
expect(console.warn).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw if the provided config does not exist in the tree', () => {
|
it('should throw if the provided config does not exist in the tree', () => {
|
||||||
|
|||||||
@ -19,6 +19,10 @@ export function addPropertyToJestConfig(
|
|||||||
propertyName: string,
|
propertyName: string,
|
||||||
value: unknown
|
value: unknown
|
||||||
) {
|
) {
|
||||||
|
if (!host.exists(path)) {
|
||||||
|
throw new Error(`Cannot find '${path}' in your workspace.`);
|
||||||
|
}
|
||||||
|
try {
|
||||||
const configObject = jestConfigObjectAst(host, path);
|
const configObject = jestConfigObjectAst(host, path);
|
||||||
const properties = propertyName.split('.');
|
const properties = propertyName.split('.');
|
||||||
const changes = addOrUpdateProperty(
|
const changes = addOrUpdateProperty(
|
||||||
@ -28,6 +32,14 @@ export function addPropertyToJestConfig(
|
|||||||
path
|
path
|
||||||
);
|
);
|
||||||
insert(host, path, changes);
|
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,
|
path: string,
|
||||||
propertyName: string
|
propertyName: string
|
||||||
) {
|
) {
|
||||||
|
if (!host.exists(path)) {
|
||||||
|
throw new Error(`Cannot find '${path}' in your workspace.`);
|
||||||
|
}
|
||||||
|
try {
|
||||||
const configObject = jestConfigObjectAst(host, path);
|
const configObject = jestConfigObjectAst(host, path);
|
||||||
const propertyAssignment = removeProperty(
|
const propertyAssignment = removeProperty(
|
||||||
configObject,
|
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}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user