Skip to content

Commit

Permalink
Tweak tests to run against packed and installed ts-node instead of lo…
Browse files Browse the repository at this point in the history
…cal dev tree
  • Loading branch information
cspotcode committed May 10, 2020
1 parent c1ae9a0 commit 95cb9bc
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 8 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions src/externs.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module 'pify' {
const _export: typeof import('util').promisify
export = _export
}
34 changes: 27 additions & 7 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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`, {
Expand Down Expand Up @@ -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')
Expand Down
4 changes: 3 additions & 1 deletion tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
!node_modules/
!from-node-modules/node_modules/
package-lock.json
ts-node-packed.tgz
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"ts-node": "file:ts-node-packed.tgz"
}
}

0 comments on commit 95cb9bc

Please sign in to comment.