-
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve test coverage (happy path, npm failure) #12
Conversation
52950c1
to
31d76ad
Compare
193dbb4
to
d294baa
Compare
d294baa
to
6fba076
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Running into a problem with making this test work on CI (see the PR description). Any advice or help is appreciated!
@@ -3,5 +3,6 @@ import { defineConfig } from 'vitest/config'; | |||
export default defineConfig({ | |||
test: { | |||
include: ['**/*.test.{js,mjs,cjs}'], | |||
watchExclude: ['**/node_modules/**', 'test/fixtures/**'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added to prevent an infinite cycle of test fixture regeneration.
How about using invalid {
"dependencies": {
"is-obj": "0.0.1"
}
}
|
Ah! That's a great suggestion, thank you! Have done that exact example and the tests pass on CI. Appreciate the advice! I'll mark this PR ready for review. |
Thanks, @mattxwang. In addition, I got an idea to refactor the cleanup code. Like this: // integration.test.js
import * as fs from 'node:fs';
import * as path from 'node:path';
import { execFileSync } from 'node:child_process';
function backupFiles(root) {
const pathsToBackup = [inputs.failNpmInstall, inputs.validEnv];
for (const pathToTest of pathsToBackup) {
fs.copyFileSync(
path.join(root, pathToTest, 'package.json'),
path.join(root, pathToTest, 'package.json.bak'),
);
}
}
function cleanupGenFiles(root) {
const pathsToCleanup = [inputs.failNpmInstall, inputs.validEnv];
for (const pathToTest of pathsToCleanup) {
for (const file of ['.stylelintrc.json', 'package-lock.json', 'node_modules']) {
fs.rmSync(path.join(root, pathToTest, file), { recursive: true, force: true });
}
fs.renameSync(
path.join(root, pathToTest, 'package.json.bak'),
path.join(root, pathToTest, 'package.json'),
);
}
}
describe('stylelint-create', () => {
beforeEach((context) => {
backupFiles(getProjectRoot(context));
});
afterEach((context) => {
cleanupGenFiles(getProjectRoot(context));
});
// ...
}); Keys:
What do you think about this suggestion? |
…b instead of shell scripts Co-authored-by: Masafumi Koba <[email protected]>
It's wonderful - definitely agree that using JS APIs instead of the shell is a good idea, both for maintenance and reliability (shell and Windows can be annoying). A runtime backup is also nice, since we now only have one source of truth. Tried out your code snippet, added you as a co-author; looks like it passes both locally and on CI. Thank you for the suggestion! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. LGTM 👍🏼
Closes #9.
Figuring out the npm failure case is a bit of a headscratcher. The only approach I could think of is making one of the install-related scripts a failure.Resolved with @ybiquitous's suggestion below of using an invalid dependency.
Separately, the happy path test is a bit unorthodox in that it actually does run
npm install
. I think it would be better to do this with a dry run, but currently there's no way for me to pass that argument in tonpm
; I can implement a dry-run feature for stylelint-create after this PR.