From 95cb9bc5a66d15a227045553c833c02a5b8b11b9 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sun, 10 May 2020 15:56:13 -0400 Subject: [PATCH] Tweak tests to run against packed and installed ts-node instead of local dev tree --- package-lock.json | 5 +++ package.json | 1 + src/externs.d.ts | 4 +++ src/index.spec.ts | 34 +++++++++++++++---- tests/.gitignore | 4 ++- .../from-node-modules.ts | 0 .../node_modules/test.ts | 0 tests/package.json | 5 +++ 8 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 src/externs.d.ts rename tests/{ => from-node-modules}/from-node-modules.ts (100%) rename tests/{ => from-node-modules}/node_modules/test.ts (100%) create mode 100644 tests/package.json diff --git a/package-lock.json b/package-lock.json index dc05ff3c6..ab41169c7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1170,6 +1170,11 @@ "integrity": "sha1-uULm1L3mUwBe9rcTYd74cn0GReA=", "dev": true }, + "pify": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/pify/-/pify-5.0.0.tgz", + "integrity": "sha512-eW/gHNMlxdSP6dmG6uJip6FXN0EQBwm2clYYd8Wul42Cwu/DK8HEftzsapcNdYe2MfLiIwZqsDk2RDEsTE79hA==" + }, "prelude-ls": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.1.2.tgz", diff --git a/package.json b/package.json index 4c4eb65f0..3047d6bb5 100644 --- a/package.json +++ b/package.json @@ -70,6 +70,7 @@ "istanbul": "^0.4.0", "mocha": "^6.2.2", "ntypescript": "^1.201507091536.1", + "pify": "^5.0.0", "proxyquire": "^2.0.0", "react": "^16.0.0", "rimraf": "^3.0.0", diff --git a/src/externs.d.ts b/src/externs.d.ts new file mode 100644 index 000000000..f44c08f51 --- /dev/null +++ b/src/externs.d.ts @@ -0,0 +1,4 @@ +declare module 'pify' { + const _export: typeof import('util').promisify + export = _export +} diff --git a/src/index.spec.ts b/src/index.spec.ts index 355cfcdf3..041be7a6b 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -5,16 +5,36 @@ import semver = require('semver') import ts = require('typescript') import proxyquire = require('proxyquire') import { register, create, VERSION } from './index' +import { mkdtempSync, readdirSync, copyFileSync, rmdirSync, unlinkSync, existsSync } from 'fs' +import * as promisify from 'pify' +const execP = promisify(exec) + +const ROOT_DIR = join(__dirname, '..') const TEST_DIR = join(__dirname, '../tests') +const TARBALL_PATH = join(TEST_DIR, 'ts-node-packed.tgz') const PROJECT = join(TEST_DIR, 'tsconfig.json') -const BIN_PATH = join(__dirname, '../dist/bin') -const BIN_SCRIPT_PATH = join(__dirname, '../dist/bin-script') +const BIN_PATH = join(TEST_DIR, 'node_modules/.bin/ts-node') +const BIN_SCRIPT_PATH = join(TEST_DIR, 'node_modules/.bin/ts-node-script') const SOURCE_MAP_REGEXP = /\/\/# sourceMappingURL=data:application\/json;charset=utf\-8;base64,[\w\+]+=*$/ +// Pack and install ts-node locally, necessary to test package "exports" +before(async function () { + this.timeout(30000) + const tempDir = mkdtempSync(join(TEST_DIR, 'tmp')) + await execP(`npm pack "${ROOT_DIR}"`, { cwd: tempDir }) + const tarballPath = join(tempDir, readdirSync(tempDir)[0]) + copyFileSync(tarballPath, TARBALL_PATH) + unlinkSync(tarballPath) + rmdirSync(tempDir) + await execP(`npm install`, { cwd: TEST_DIR }) + const packageLockPath = join(TEST_DIR, 'package-lock.json') + existsSync(packageLockPath) && unlinkSync(packageLockPath) +}) + describe('ts-node', function () { - const cmd = `node "${BIN_PATH}" --project "${PROJECT}"` + const cmd = `"${BIN_PATH}" --project "${PROJECT}"` this.timeout(10000) @@ -35,7 +55,7 @@ describe('ts-node', function () { }) it('should register via cli', function (done) { - exec(`node -r ../register hello-world.ts`, { + exec(`node -r ts-node/register hello-world.ts`, { cwd: TEST_DIR }, function (err, stdout) { expect(err).to.equal(null) @@ -73,7 +93,7 @@ describe('ts-node', function () { }) it('should provide registered information on register', function (done) { - exec(`node -r ../register env.ts`, { + exec(`node -r ts-node/register env.ts`, { cwd: TEST_DIR }, function (err, stdout) { expect(err).to.equal(null) @@ -408,7 +428,7 @@ describe('ts-node', function () { } describe('should read ts-node options from tsconfig.json', function () { - const BIN_EXEC = `node "${join(__dirname, '../dist/bin')}" --project tests/tsconfig-options/tsconfig.json` + const BIN_EXEC = `"${BIN_PATH}" --project tests/tsconfig-options/tsconfig.json` it('should override compiler options from env', function (done) { exec(`${BIN_EXEC} tests/tsconfig-options/log-options.js`, { @@ -481,7 +501,7 @@ describe('ts-node', function () { }) it('should give ts error for invalid node_modules', function (done) { - exec(`${cmd} --compiler-host --skip-ignore tests/from-node-modules`, function (err, stdout) { + exec(`${cmd} --compiler-host --skip-ignore tests/from-node-modules/from-node-modules`, function (err, stdout) { if (err === null) return done('Expected an error') expect(err.message).to.contain('Unable to compile file from external library') diff --git a/tests/.gitignore b/tests/.gitignore index ddf342489..1f16711ae 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -1 +1,3 @@ -!node_modules/ +!from-node-modules/node_modules/ +package-lock.json +ts-node-packed.tgz diff --git a/tests/from-node-modules.ts b/tests/from-node-modules/from-node-modules.ts similarity index 100% rename from tests/from-node-modules.ts rename to tests/from-node-modules/from-node-modules.ts diff --git a/tests/node_modules/test.ts b/tests/from-node-modules/node_modules/test.ts similarity index 100% rename from tests/node_modules/test.ts rename to tests/from-node-modules/node_modules/test.ts diff --git a/tests/package.json b/tests/package.json new file mode 100644 index 000000000..26351d2b8 --- /dev/null +++ b/tests/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "ts-node": "file:ts-node-packed.tgz" + } +}