Skip to content

Commit

Permalink
feat: use jest -u if project has anti-tamper check
Browse files Browse the repository at this point in the history
If the project CI runs with anti-tamper check, then it is safe to always
run jest with --updateSnapshot.
  • Loading branch information
Elad Ben-Israel committed Jul 29, 2020
1 parent f0f73d3 commit aa85cde
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- run: yarn build
- name: Anti-tamper check
run: git diff --exit-code
- run: git push --follow-tags origin master
- run: git push --follow-tags origin $GITHUB_REF
- name: Upload artifact
uses: actions/upload-artifact@v2.1.1
with:
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"scripts": {
"projen": "node .projenrc.js && yarn install",
"projen:upgrade": "yarn upgrade -L projen && yarn projen",
"test": "yarn eslint && jest --passWithNoTests",
"test": "yarn eslint && jest --passWithNoTests --updateSnapshot",
"no-changes": "(git log --oneline -1 | grep -q \"chore(release):\") && echo \"No changes to release.\"",
"bump": "yarn --silent no-changes || standard-version",
"release": "yarn --silent no-changes || (yarn bump && git push --follow-tags origin master)",
Expand All @@ -23,7 +23,7 @@
"build": "yarn compile && yarn test && yarn run package",
"eslint": "eslint . --ext .ts",
"test:watch": "jest --watch",
"test:update": "jest -u",
"test:update": "jest --updateSnapshot",
"compat": "npx jsii-diff npm:$(node -p \"require('./package.json').name\") -k --ignore-file .compatignore || (echo \"\nUNEXPECTED BREAKING CHANGES: add keys such as 'removed:constructs.Node.of' to .compatignore to skip.\n\" && exit 1)",
"docgen": "jsii-docgen",
"bootstrap": "yarn install && yarn compile && yarn projen"
Expand Down
12 changes: 10 additions & 2 deletions src/jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,19 @@ export class Jest extends Construct {
};
}

project.addTestCommands('jest --passWithNoTests');
const jestOpts = [ '--passWithNoTests' ];

// if the project has anti-tamper configured, it should be safe to always run tests
// with --updateSnapshot because if we forget to commit a snapshot change the CI build will fail.
if (project.antitamper) {
jestOpts.push('--updateSnapshot');
}

project.addTestCommands(`jest ${jestOpts.join(' ')}`);

project.addScripts({
'test:watch': 'jest --watch',
'test:update': 'jest -u',
'test:update': 'jest --updateSnapshot',
});

project.addFields({ jest: this.config });
Expand Down
13 changes: 11 additions & 2 deletions src/node-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,11 @@ export class NodeProject extends Project {
public readonly minNodeVersion?: string;
public readonly maxNodeVersion?: string;

/**
* Indicates if workflows have anti-tamper checks.
*/
public readonly antitamper: boolean;

protected readonly npmDistTag: string;

constructor(options: NodeProjectOptions) {
Expand Down Expand Up @@ -432,6 +437,10 @@ export class NodeProject extends Project {
this._version = new Version(this);
this.manifest.version = this.version;

// indicate if we have anti-tamper configured in our workflows. used by e.g. Jest
// to decide if we can always run with --updateSnapshot
this.antitamper = (options.buildWorkflow ?? true) && (options.antitamper ?? true);

if (options.buildWorkflow ?? true) {
this.buildWorkflow = new NodeBuildWorkflow(this, 'Build', {
trigger: {
Expand All @@ -441,7 +450,7 @@ export class NodeProject extends Project {
nodeVersion: options.workflowNodeVersion ?? this.minNodeVersion,
bootstrapSteps: options.workflowBootstrapSteps,
image: options.workflowContainerImage,
antitamper: options.antitamper,
antitamper: this.antitamper,
});
}

Expand All @@ -465,7 +474,7 @@ export class NodeProject extends Project {
nodeVersion: options.workflowNodeVersion ?? this.minNodeVersion,
bootstrapSteps: options.workflowBootstrapSteps,
image: options.workflowContainerImage,
antitamper: options.antitamper,
antitamper: this.antitamper,
});

if (options.releaseToNpm) {
Expand Down

0 comments on commit aa85cde

Please sign in to comment.