question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`projen test` updates snapshots by default

See original GitHub issue

Given a TypeScriptAppProject, yarn test does the following:

$ npx projen test
....
šŸ¤– test | jest --passWithNoTests --all --updateSnapshot

The --updateSnapshot is dangerous: if I run this in CI, yarn test will not actually validate my snapshots, but happily update them in place even if my tests should by all rights have failed.

The answer is not ā€œanti-tamper protectionā€: that will tell me that files have changed, it will not give me the nice error message and diff that jest would have given me if the snapshot test was allowed to fail in the first place. Plus, you can’t assume that the tests will always be run with the synthesized GitHub workflow that includes anti-tamper protection: it might equally well run in a CodePipeline/CodeBuild setup, and that job might not even have access to the git repo to compare for any diffs.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:5
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
5nafucommented, Apr 13, 2022

@jtnz

Today I tried improving on @caviliar 's solution with:

Thanks for your suggestion, but unfortunately, this did not work for us. In the latest Version of projen, the step.exec is read only.

We used your suggestion and migrated it to:

// Create new task eslint:update that has the same exec as eslint
const eslintTask = project.tasks.tryFind('eslint');
const eslintUpdate = project.tasks.addTask('eslint:update', {
  exec: eslintTask!.steps[0]!.exec!,
});
const testUpdate = project.tasks.tryFind('test:update');
testUpdate!.spawn(eslintUpdate);

// Remove '--fix' (write) of eslint task
const newEslintCommand = eslintTask!.steps[0]!.exec!.replace(' --fix', '');
eslintTask!.reset(newEslintCommand);

const testTask = project.tasks.tryFind('test');
// Remove '--updateSnapshot' from test task
const newTestCommand = testTask!.steps[0]!.exec!.replace(' --updateSnapshot', '');
console.log(newTestCommand);
testTask!.reset(newTestCommand);
testTask!.spawn(eslintTask!);

Additionally for others trying this out, please add it before project.synth(); 😁

3reactions
Chriscbrcommented, Mar 17, 2022

Here’s a proposal - let me know if this makes sense.

Current way things work:

  1. There’s a test task which intentionally updates files. There’s also a test:update task (in JS-based project types), but it currently does the same thing.
  2. The mutableBuild option determines whether to add a GitHub workflow step to push snapshot changes / eslint fixes back to your PR as a new commit (if you’re not using GitHub, it does nothing).

Proposed way things should work:

  1. test task does not intentionally update files. test:update task does intentionally update files (by passing the appropriate flags to jest and eslint etc).
  2. The mutableBuild option is split into mutableBuildTask and mutableBuildWorkflow. mutableBuildTask will determine whether the default build task runs test or test:update (irrespective of whether you’re running them locally or in CI/CD). mutableBuildWorkflow will do what mutableBuild did, but will also do nothing if mutableBuildTask is false.

In the future, mutableBuildTask: false should also cause running npx projen in CICD to fail immediately if it detects any configuration files that need to be updated, but this requires some more technical changes to projen’s synthesis engine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

API Reference | projen
Default : true; editGitignore ( boolean ) Update the project's .gitignore file. ... A Testing static class with a .synth helper for getting...
Read more >
Projen: NodeJS project boilerplating | by Mykhailo Gorianskyi
test :update Update jest snapshots ... By default typescript project of projen does 4 things for build command:.
Read more >
How to simplify your next software project set-up - TIQQE
Most times it comes with some sane defaults, so you kind of get batteries included experience. ... projen test:update Update jest snapshots.
Read more >
A Beginner's Guide to Create AWS CDK ... - DEV Community ā€ ā€
You will see that projen automatically generates the package.json, ... 3 total Snapshots: 0 total Time: 10.121 s Ran all test suites.
Read more >
A Beginner's Guide to Create AWS CDK ... - hayao-k.dev
projen does not only generate various files during project creation but also continuously updates and maintains these settings. You can easilyĀ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found