-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(tests): added more test cases to improve code coverage
- updated readme file with minor changes - added pending changelog version for notes - refactored tsconfig functions into a class for easier test case stubbing
- Loading branch information
Showing
7 changed files
with
226 additions
and
63 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export function assertNotUndefined<T> (value: T | undefined): T { | ||
if (value === undefined) { | ||
throw new TypeError('Expected value to !== undefined.') | ||
} | ||
return value | ||
} |
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,41 +1,42 @@ | ||
import * as sinon from 'sinon' | ||
import { assert } from 'chai' | ||
import * as tsconfigLib from '../src/tsconfig' | ||
import { assertNotUndefined } from './assert' | ||
import { TSConfig } from '../src/tsconfig' | ||
import * as optionsLib from '../src/options' | ||
|
||
describe('CLI', (): void => { | ||
let getTSConfigFileStub: sinon.SinonStub<any, any> | ||
describe('options', () => { | ||
describe('getOptions', (): void => { | ||
let getTSConfigFileStub: sinon.SinonStub<any, any> | ||
|
||
before((): void => { | ||
getTSConfigFileStub = sinon.stub(tsconfigLib, 'getTSConfigFile') | ||
}) | ||
before((): void => { | ||
getTSConfigFileStub = sinon.stub(TSConfig.prototype, 'getConfigFilePath') | ||
}) | ||
|
||
after((): void => { | ||
sinon.restore() | ||
}) | ||
after((): void => { | ||
sinon.restore() | ||
}) | ||
|
||
afterEach((): void => { | ||
sinon.resetHistory() | ||
sinon.resetBehavior() | ||
}) | ||
afterEach((): void => { | ||
sinon.resetHistory() | ||
sinon.resetBehavior() | ||
}) | ||
|
||
it('should return the correct options', async (): Promise<void> => { | ||
const projectPath = 'some/project.path' | ||
getTSConfigFileStub.returns(projectPath) | ||
const options = await optionsLib.getOptions() | ||
assert.equal(options.cmd, 'ts-standard') | ||
assert.equal(options.version, require('../package.json').version) | ||
assert.equal(options.homepage, require('../package.json').homepage) | ||
assert.equal(options.bugs, require('../package.json').bugs.url) | ||
assert.equal(options.tagline, 'Standard for Typescript!') | ||
assert.ok(options.eslint) | ||
assert.ok(options.eslintConfig.configFile) | ||
if (options.eslintConfig.configFile !== undefined) { | ||
it('should return the correct options', async (): Promise<void> => { | ||
const projectPath = 'some/project.path' | ||
getTSConfigFileStub.returns(projectPath) | ||
const options = await optionsLib.getOptions() | ||
assert.equal(options.cmd, 'ts-standard') | ||
assert.equal(options.version, require('../package.json').version) | ||
assert.equal(options.homepage, require('../package.json').homepage) | ||
assert.equal(options.bugs, require('../package.json').bugs.url) | ||
assert.equal(options.tagline, 'Standard for Typescript!') | ||
assert.ok(options.eslint) | ||
assert.ok(options.eslintConfig.configFile) | ||
options.eslintConfig.configFile = assertNotUndefined(options.eslintConfig.configFile) | ||
assert.match(options.eslintConfig.configFile, /eslintrc\.json/gi) | ||
} | ||
assert.ok(options.eslintConfig.parserOptions) | ||
if (options.eslintConfig.parserOptions !== undefined) { | ||
assert.ok(options.eslintConfig.parserOptions) | ||
options.eslintConfig.parserOptions = assertNotUndefined(options.eslintConfig.parserOptions) | ||
assert.equal(options.eslintConfig.parserOptions.project, projectPath) | ||
} | ||
}) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import * as sinon from 'sinon' | ||
import { assert } from 'chai' | ||
import { join } from 'path' | ||
import * as pkgConfLib from 'pkg-conf' | ||
import { assertNotUndefined } from './assert' | ||
import { TSConfig } from '../src/tsconfig' | ||
|
||
describe('tsconfig', () => { | ||
let tsConfig: TSConfig | ||
before(() => { | ||
tsConfig = new TSConfig() | ||
}) | ||
|
||
describe('_isValidPath', () => { | ||
it('should return true for a valid path', () => { | ||
const validPath = join(__dirname, '/options.spec.ts') | ||
const isValid = tsConfig._isValidPath(validPath) | ||
assert.isTrue(isValid) | ||
}) | ||
|
||
it('should return false for an invalid path', () => { | ||
const invalidPath = join(__dirname, '/non-existent-file.ts') | ||
const isValid = tsConfig._isValidPath(invalidPath) | ||
assert.isFalse(isValid) | ||
}) | ||
}) | ||
|
||
describe('_getTSConfigFromDefaultLocations', (): void => { | ||
let _isValidPathStub: sinon.SinonStub<any, any> | ||
|
||
before((): void => { | ||
_isValidPathStub = sinon.stub(tsConfig, '_isValidPath') | ||
}) | ||
|
||
after((): void => { | ||
sinon.restore() | ||
}) | ||
|
||
afterEach((): void => { | ||
sinon.resetHistory() | ||
sinon.resetBehavior() | ||
}) | ||
|
||
it('should return a valid path if one is found found', (): void => { | ||
_isValidPathStub.returns(true) | ||
let validPath = tsConfig._getTSConfigFromDefaultLocations() | ||
validPath = assertNotUndefined(validPath) | ||
assert.match(validPath, /tsconfig\.eslint\.json/gi) | ||
}) | ||
|
||
it('should return the first path found if a valid path is found', (): void => { | ||
_isValidPathStub.returns(false) | ||
_isValidPathStub.onSecondCall().returns(true) | ||
let validPath = tsConfig._getTSConfigFromDefaultLocations() | ||
validPath = assertNotUndefined(validPath) | ||
assert.match(validPath, /tsconfig\.json/gi) | ||
}) | ||
|
||
it('should return undefined if no valid path found', (): void => { | ||
_isValidPathStub.returns(false) | ||
const validPath = tsConfig._getTSConfigFromDefaultLocations() | ||
assert.isUndefined(validPath) | ||
}) | ||
}) | ||
|
||
describe('_getTSConfigPathFromSettings', (): void => { | ||
let _pkgConfStub: sinon.SinonStub<any, any> | ||
|
||
before((): void => { | ||
_pkgConfStub = sinon.stub(pkgConfLib, 'default') | ||
}) | ||
|
||
after((): void => { | ||
sinon.restore() | ||
}) | ||
|
||
afterEach((): void => { | ||
sinon.resetHistory() | ||
sinon.resetBehavior() | ||
}) | ||
|
||
it('should return a valid path if one is found found', async (): Promise<void> => { | ||
_pkgConfStub.returns({ project: './tsconfig.eslint.json' }) | ||
let validPath = await tsConfig._getTSConfigPathFromSettings() | ||
validPath = assertNotUndefined(validPath) | ||
assert.match(validPath, /tsconfig.eslint.json/gi) | ||
}) | ||
|
||
it('should return undefined if no valid path found', async (): Promise<void> => { | ||
_pkgConfStub.returns({ project: './non-existent-file.ts' }) | ||
const validPath = await tsConfig._getTSConfigPathFromSettings() | ||
assert.isUndefined(validPath) | ||
}) | ||
|
||
it('should return undefined if project settings provided', async (): Promise<void> => { | ||
_pkgConfStub.returns({}) | ||
const validPath = await tsConfig._getTSConfigPathFromSettings() | ||
assert.isUndefined(validPath) | ||
}) | ||
}) | ||
|
||
describe('getConfigFilePath', (): void => { | ||
let _getTSConfigPathFromSettingsStub: sinon.SinonStub<any, any> | ||
let _getTSConfigFromDefaultLocationsStub: sinon.SinonStub<any, any> | ||
const packageJsonPath = '/path/from/package.json' | ||
const defaultLocationPath = '/path/from/default-location.json' | ||
|
||
before((): void => { | ||
_getTSConfigPathFromSettingsStub = sinon.stub(tsConfig, '_getTSConfigPathFromSettings') | ||
_getTSConfigFromDefaultLocationsStub = sinon.stub(tsConfig, '_getTSConfigFromDefaultLocations') | ||
}) | ||
|
||
after((): void => { | ||
sinon.restore() | ||
}) | ||
|
||
afterEach((): void => { | ||
sinon.resetHistory() | ||
sinon.resetBehavior() | ||
}) | ||
|
||
it('should return a valid path from package.json first', async (): Promise<void> => { | ||
_getTSConfigPathFromSettingsStub.returns(packageJsonPath) | ||
_getTSConfigFromDefaultLocationsStub.returns(defaultLocationPath) | ||
let validPath = await tsConfig.getConfigFilePath() | ||
validPath = assertNotUndefined(validPath) | ||
assert.equal(validPath, packageJsonPath) | ||
}) | ||
|
||
it('should return a valid path from default location if no package.json setting found', async (): Promise<void> => { | ||
_getTSConfigPathFromSettingsStub.returns(undefined) | ||
_getTSConfigFromDefaultLocationsStub.returns(defaultLocationPath) | ||
let validPath = await tsConfig.getConfigFilePath() | ||
validPath = assertNotUndefined(validPath) | ||
assert.equal(validPath, defaultLocationPath) | ||
}) | ||
|
||
it('should return undefined if no valid paths found', async (): Promise<void> => { | ||
_getTSConfigPathFromSettingsStub.returns(undefined) | ||
_getTSConfigFromDefaultLocationsStub.returns(undefined) | ||
const validPath = await tsConfig.getConfigFilePath() | ||
assert.isUndefined(validPath) | ||
}) | ||
}) | ||
}) |