From 7cb6d570f531fd7efaa83a526b68610abd927dab Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Wed, 12 Aug 2020 19:29:17 -0400 Subject: [PATCH] Add test coverage (#1109) * WIP * fix lint failures * Add tests --- src/index.spec.ts | 59 ++++++++++++++++++++++++++++- tests/esm-import-http-url/index.mjs | 1 + 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tests/esm-import-http-url/index.mjs diff --git a/src/index.spec.ts b/src/index.spec.ts index c3def2b5e..7a175c9c1 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -39,6 +39,7 @@ before(async function () { describe('ts-node', function () { const cmd = `"${BIN_PATH}" --project "${PROJECT}"` + const cmdNoProject = `"${BIN_PATH}"` this.timeout(10000) @@ -88,6 +89,32 @@ describe('ts-node', function () { }) }) + it('shows usage via --help', function (done) { + exec(`${cmdNoProject} --help`, function (err, stdout) { + expect(err).to.equal(null) + expect(stdout).to.match(/Usage: ts-node /) + return done() + }) + }) + it('shows version via -v', function (done) { + exec(`${cmdNoProject} -v`, function (err, stdout) { + expect(err).to.equal(null) + expect(stdout.trim()).to.equal('v' + testsDirRequire('ts-node/package').version) + return done() + }) + }) + it('shows version of compiler via -vv', function (done) { + exec(`${cmdNoProject} -vv`, function (err, stdout) { + expect(err).to.equal(null) + expect(stdout.trim()).to.equal( + `ts-node v${ testsDirRequire('ts-node/package').version }\n` + + `node ${ process.version }\n` + + `compiler v${ testsDirRequire('typescript/package').version }` + ) + return done() + }) + }) + it('should register via cli', function (done) { exec(`node -r ts-node/register hello-world.ts`, { cwd: TEST_DIR @@ -727,11 +754,30 @@ describe('ts-node', function () { }) describe('create', () => { + let service: tsNodeTypes.Register + before(() => { + service = create({ compilerOptions: { target: 'es5' }, skipProject: true }) + }) + it('should create generic compiler instances', () => { - const service = create({ compilerOptions: { target: 'es5' }, skipProject: true }) const output = service.compile('const x = 10', 'test.ts') expect(output).to.contain('var x = 10;') }) + + describe('should get type information', () => { + it('given position of identifier', () => { + expect(service.getTypeInfo('/**jsdoc here*/const x = 10', 'test.ts', 21)).to.deep.equal({ + comment: 'jsdoc here', + name: 'const x: 10' + }) + }) + it('given position that does not point to an identifier', () => { + expect(service.getTypeInfo('/**jsdoc here*/const x = 10', 'test.ts', 0)).to.deep.equal({ + comment: '', + name: '' + }) + }) + }) }) describe('issue #1098', () => { @@ -824,6 +870,17 @@ describe('ts-node', function () { }) }) + it('defers to fallback loaders when URL should not be handled by ts-node', function (done) { + exec(`${cmd} index.mjs`, { + cwd: join(__dirname, '../tests/esm-import-http-url') + }, function (err, stdout, stderr) { + expect(err).to.not.equal(null) + // expect error from node's default resolver + expect(stderr).to.match(/Error \[ERR_UNSUPPORTED_ESM_URL_SCHEME\]:.*\n *at defaultResolve/) + return done() + }) + }) + it('should support transpile only mode via dedicated loader entrypoint', (done) => { exec(`${cmd}/transpile-only index.ts`, { cwd: join(__dirname, '../tests/esm-transpile-only') }, function (err, stdout) { expect(err).to.equal(null) diff --git a/tests/esm-import-http-url/index.mjs b/tests/esm-import-http-url/index.mjs new file mode 100644 index 000000000..35e59a6c6 --- /dev/null +++ b/tests/esm-import-http-url/index.mjs @@ -0,0 +1 @@ +import 'http://example.com/this-url-should-be-ignored-by-our-esm-loader.js'