Skip to content

Commit

Permalink
Add --test-script flag to run custom tests before publish (#557)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
dopecodez and sindresorhus committed Aug 9, 2020
1 parent 0638d27 commit 0c46a2f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
21 changes: 21 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ $ np --help
--no-yarn Don't use Yarn
--contents Subdirectory to publish
--no-release-draft Skips opening a GitHub release draft
--test-script Name of npm run script to run tests before publishing (default: test)
--no-2fa Don't enable 2FA on new packages (not recommended)
Examples
Expand Down Expand Up @@ -94,6 +95,7 @@ Currently, these are the flags you can configure:
- `yarn` - Use yarn if possible (`true` by default).
- `contents` - Subdirectory to publish (`.` by default).
- `releaseDraft` - Open a GitHub release draft after releasing (`true` by default).
- `testScript` - Name of npm run script to run tests before publishing (`test` by default).
- `2fa` - Enable 2FA on new packages (`true` by default) (it's not recommended to set this to `false`).

For example, this configures `np` to never use Yarn and to use `dist` as the subdirectory to publish:
Expand Down Expand Up @@ -160,6 +162,25 @@ You can also add `np` to a custom script in `package.json`. This can be useful i
}
```

### User-defined tests

If you want to run a user-defined test script before publishing instead of the normal `npm test` or `yarn test`, you can use `--test-script` flag or the `testScript` config. This is can be useful when your normal test script is running with a `--watch` mode or in case you want to run some specific tests (maybe on the packaged files) before publishing.

For example, `np --test-script=publish-test` would run the `publish-test` script instead of the default `test`.

```json
{
"name": "my-awesome-package",
"scripts": {
"test": "ava --watch",
"publish-test": "ava"
},
"devDependencies": {
"np": "*"
}
}
```

### Signed Git tag

Set the [`sign-git-tag`](https://docs.npmjs.com/misc/config#sign-git-tag) npm config to have the Git tag signed:
Expand Down
4 changes: 4 additions & 0 deletions source/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const cli = meow(`
--no-yarn Don't use Yarn
--contents Subdirectory to publish
--no-release-draft Skips opening a GitHub release draft
--test-script Name of npm run script to run tests before publishing (default: test)
--no-2fa Don't enable 2FA on new packages (not recommended)
Examples
Expand Down Expand Up @@ -76,6 +77,9 @@ const cli = meow(`
preview: {
type: 'boolean'
},
testScript: {
type: 'string'
},
'2fa': {
type: 'boolean'
}
Expand Down
8 changes: 5 additions & 3 deletions source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ module.exports = async (input = 'patch', options) => {
const rootDir = pkgDir.sync();
const hasLockFile = fs.existsSync(path.resolve(rootDir, options.yarn ? 'yarn.lock' : 'package-lock.json')) || fs.existsSync(path.resolve(rootDir, 'npm-shrinkwrap.json'));
const isOnGitHub = options.repoUrl && (hostedGitInfo.fromUrl(options.repoUrl) || {}).type === 'github';
const testScript = options.testScript || 'test';
const testCommand = options.testScript ? ['run', testScript] : [testScript];

let publishStatus = 'UNKNOWN';

Expand Down Expand Up @@ -145,14 +147,14 @@ module.exports = async (input = 'patch', options) => {
{
title: 'Running tests using npm',
enabled: () => options.yarn === false,
task: () => exec('npm', ['test'])
task: () => exec('npm', testCommand)
},
{
title: 'Running tests using Yarn',
enabled: () => options.yarn === true,
task: () => exec('yarn', ['test']).pipe(
task: () => exec('yarn', testCommand).pipe(
catchError(error => {
if (error.message.includes('Command "test" not found')) {
if (error.message.includes(`Command ${testScript} not found`)) {
return [];
}

Expand Down

0 comments on commit 0c46a2f

Please sign in to comment.