diff --git a/readme.md b/readme.md index 8ce4ceeb..9b23f4ad 100644 --- a/readme.md +++ b/readme.md @@ -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 @@ -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: @@ -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: diff --git a/source/cli.js b/source/cli.js index 92b09c15..eb9e0190 100755 --- a/source/cli.js +++ b/source/cli.js @@ -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 @@ -76,6 +77,9 @@ const cli = meow(` preview: { type: 'boolean' }, + testScript: { + type: 'string' + }, '2fa': { type: 'boolean' } diff --git a/source/index.js b/source/index.js index e70c9983..4a7444d5 100644 --- a/source/index.js +++ b/source/index.js @@ -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'; @@ -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 []; }