Skip to content
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

Merged
merged 7 commits into from
Feb 14, 2023

Conversation

mattxwang
Copy link
Member

@mattxwang mattxwang commented Feb 12, 2023

Which issue, if any, is this issue related to?

Closes #9.

Is there anything in the PR that needs further explanation?

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 to npm; I can implement a dry-run feature for stylelint-create after this PR.

@mattxwang mattxwang force-pushed the improve-test-coverage branch from 52950c1 to 31d76ad Compare February 12, 2023 22:05
@mattxwang mattxwang force-pushed the improve-test-coverage branch from 193dbb4 to d294baa Compare February 12, 2023 22:31
@mattxwang mattxwang force-pushed the improve-test-coverage branch from d294baa to 6fba076 Compare February 12, 2023 22:38
Copy link
Member Author

@mattxwang mattxwang left a 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/**'],
Copy link
Member Author

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.

test/fixtures/fail-npm-install/clean-package.json Outdated Show resolved Hide resolved
@ybiquitous
Copy link
Member

How about using invalid dependencies instead of scripts.{prepare,preinstall} hooks? For example:

{
  "dependencies": {
    "is-obj": "0.0.1"
  }
}

is-obj does NOT have a 0.0.1 version.

@mattxwang
Copy link
Member Author

How about using invalid dependencies instead of scripts.{prepare,preinstall} hooks?

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.

@mattxwang mattxwang marked this pull request as ready for review February 13, 2023 16:55
@ybiquitous
Copy link
Member

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:

  • Back up package.json at runtime in beforeEach()
  • Clean up files via Node.js APIs instead of Shell script
    • I believe we're more familiar with JS than Shell script, and easier to find the clean-up code in the same file
  • Use context in beforeEach and afterEach hooks instead of __dirname

What do you think about this suggestion?

…b instead of shell scripts

Co-authored-by: Masafumi Koba <[email protected]>
@mattxwang
Copy link
Member Author

What do you think about this suggestion?

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!

Copy link
Member

@ybiquitous ybiquitous left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. LGTM 👍🏼

@mattxwang mattxwang merged commit 52dbc0a into main Feb 14, 2023
@mattxwang mattxwang deleted the improve-test-coverage branch February 14, 2023 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve test coverage
2 participants