-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Move production check logic into the runner itself
- Loading branch information
1 parent
c21603b
commit c21d24b
Showing
5 changed files
with
134 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,100 @@ | ||
import { CLIArgs, getEnvFilePath } from '../../src/runner'; | ||
import consola from 'consola'; | ||
|
||
const getContext = (overrides: Record<string, unknown> = {}): CLIArgs => ({ | ||
import * as runner from '../../src/runner'; | ||
|
||
const getContext = (overrides: Record<string, unknown> = {}): runner.CLIArgs => ({ | ||
_: ['test'], | ||
$0: 'runner.spec.ts', | ||
...overrides, | ||
}); | ||
|
||
describe('unit.runner', () => { | ||
let currentExitCode = process.exitCode; | ||
|
||
beforeAll(() => { | ||
currentExitCode = process.exitCode; | ||
}); | ||
|
||
afterEach(() => { | ||
process.exitCode = currentExitCode; | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('getEnvFilePath', () => { | ||
it('resolves the supplied --env-path=.env', () => { | ||
const context = getContext({ 'env-path': '.env' }); | ||
const actual = getEnvFilePath(context); | ||
const actual = runner.getEnvFilePath(context); | ||
expect(actual).toMatch(/^.+\/.env$/); | ||
}); | ||
|
||
it('resolves the supplied --env-path=./.env', () => { | ||
const context = getContext({ 'env-path': './.env' }); | ||
const actual = getEnvFilePath(context); | ||
const actual = runner.getEnvFilePath(context); | ||
expect(actual).toMatch(/^.+\/.env$/); | ||
}); | ||
|
||
it('resolves the supplied --env-path=path/.env', () => { | ||
const context = getContext({ 'env-path': 'path/.env' }); | ||
const actual = getEnvFilePath(context); | ||
const actual = runner.getEnvFilePath(context); | ||
expect(actual).toMatch(/^.+\/path\/.env$/); | ||
}); | ||
|
||
it('resolves the supplied --env-path=./path/.env', () => { | ||
const context = getContext({ 'env-path': './path/.env' }); | ||
const actual = getEnvFilePath(context); | ||
const actual = runner.getEnvFilePath(context); | ||
expect(actual).toMatch(/^.+\/path\/.env$/); | ||
}); | ||
|
||
it('returns the supplied --env-path=/etc/path/to/env as is', () => { | ||
const envPath = '/etc/path/to/env'; | ||
const context = getContext({ 'env-path': envPath }); | ||
const actual = getEnvFilePath(context); | ||
const actual = runner.getEnvFilePath(context); | ||
expect(actual).toEqual(envPath); | ||
}); | ||
}); | ||
|
||
describe('runner', () => { | ||
const getOptions = (overrides: Record<string, unknown> = {}) => ({ | ||
getArgs: () => ({ | ||
_: ['test'], | ||
$0: 'jest', | ||
...overrides, | ||
}), | ||
commands: { | ||
test: jest.fn(() => Promise.resolve(1)), | ||
}, | ||
}); | ||
|
||
describe('targeting production', () => { | ||
it('fails with any text', async () => { | ||
const options = getOptions({ stage: 'production' }); | ||
jest.spyOn(consola, 'info').mockImplementation(); | ||
jest.spyOn(consola, 'warn').mockImplementation(); | ||
jest.spyOn(consola, 'error').mockImplementation(); | ||
jest.spyOn(runner, 'promptProduction').mockImplementation(() => Promise.resolve('nope')); | ||
|
||
await runner.runner(options); | ||
|
||
expect(runner.promptProduction).toHaveBeenCalledTimes(1); | ||
expect(options.commands.test).toHaveBeenCalledTimes(0); | ||
expect(consola.warn).toHaveBeenCalledTimes(0); | ||
expect(consola.error).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it(`accepts '${runner.CONFIRM_PRODUCTION}'`, async () => { | ||
const options = getOptions({ stage: 'production' }); | ||
jest.spyOn(consola, 'info').mockImplementation(); | ||
jest.spyOn(consola, 'warn').mockImplementation(); | ||
jest.spyOn(consola, 'error').mockImplementation(); | ||
jest.spyOn(runner, 'promptProduction').mockImplementation(() => Promise.resolve(runner.CONFIRM_PRODUCTION)); | ||
|
||
await runner.runner(options); | ||
|
||
expect(runner.promptProduction).toHaveBeenCalledTimes(1); | ||
expect(options.commands.test).toHaveBeenCalledTimes(1); | ||
expect(consola.warn).toHaveBeenCalledTimes(1); | ||
expect(consola.error).toHaveBeenCalledTimes(0); | ||
}); | ||
}); | ||
}); | ||
}); |