Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run tests e2e against an npm pack && npm installed ts-node #1032

Merged
merged 6 commits into from May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions package-lock.json

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

11 changes: 7 additions & 4 deletions package.json
Expand Up @@ -22,14 +22,16 @@
"scripts": {
"lint": "tslint \"src/**/*.ts\" --project tsconfig.json",
"lint-fix": "tslint \"src/**/*.ts\" --project tsconfig.json --fix",
"clean": "rimraf dist && rimraf tsconfig.schema.json && rimraf tsconfig.schemastore-schema.json",
"build": "npm run clean && npm run build-tsc && npm run build-configSchema",
"clean": "rimraf dist && rimraf tsconfig.schema.json && rimraf tsconfig.schemastore-schema.json && rimraf tests/ts-node-packed.tgz",
"build": "npm run build-nopack && npm run build-pack",
"build-nopack": "npm run clean && npm run build-tsc && npm run build-configSchema",
"build-tsc": "tsc",
"build-configSchema": "typescript-json-schema --topRef --refs --validationKeywords allOf --out tsconfig.schema.json tsconfig.json TsConfigSchema && node --require ./register ./scripts/create-merged-schema",
"build-pack": "node ./scripts/build-pack.js",
"test-spec": "mocha dist/**/*.spec.js -R spec --bail",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- \"dist/**/*.spec.js\" -R spec --bail",
"test": "npm run build && npm run lint && npm run test-cov",
"prepare": "npm run build"
"prepare": "npm run build-nopack"
},
"engines": {
"node": ">=6.0.0"
Expand Down Expand Up @@ -77,7 +79,8 @@
"tslint": "^6.1.0",
"tslint-config-standard": "^9.0.0",
"typescript": "3.8.3",
"typescript-json-schema": "^0.42.0"
"typescript-json-schema": "^0.42.0",
"util.promisify": "^1.0.1"
},
"peerDependencies": {
"typescript": ">=2.7"
Expand Down
20 changes: 20 additions & 0 deletions scripts/build-pack.js
@@ -0,0 +1,20 @@
// Written in JS to support Windows
// Would otherwise be written as inline bash in package.json script

const { exec } = require('child_process')
const { mkdtempSync, writeFileSync, readFileSync, unlinkSync, rmdirSync, readdirSync } = require('fs')
const { join } = require('path')

const testDir = join(__dirname, '../tests')
const tarballPath = join(testDir, 'ts-node-packed.tgz')
const tempDir = mkdtempSync(join(testDir, 'tmp'))
exec(`npm pack "${join(__dirname, '..')}"`, { cwd: tempDir }, (err, stdout) => {
if (err) {
console.error(err)
process.exit(1)
}
const tempTarballPath = join(tempDir, readdirSync(tempDir)[0])
writeFileSync(tarballPath, readFileSync(tempTarballPath))
unlinkSync(tempTarballPath)
rmdirSync(tempDir)
})
4 changes: 4 additions & 0 deletions src/externs.d.ts
@@ -0,0 +1,4 @@
declare module 'util.promisify' {
const _export: typeof import('util').promisify
export = _export
}
26 changes: 19 additions & 7 deletions src/index.spec.ts
Expand Up @@ -5,16 +5,28 @@ import semver = require('semver')
import ts = require('typescript')
import proxyquire = require('proxyquire')
import { register, create, VERSION } from './index'
import { unlinkSync, existsSync } from 'fs'
import * as promisify from 'util.promisify'

const execP = promisify(exec)

const TEST_DIR = join(__dirname, '../tests')
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)
await execP(`npm install`, { cwd: TEST_DIR })
const packageLockPath = join(TEST_DIR, 'package-lock.json')
existsSync(packageLockPath) && unlinkSync(packageLockPath)
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if these a way to do this outside of the test suite or possibly a separate set of tests? Either way this looks like a great start at coverage.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can definitely move the npm pack into our npm build. That would be better since it would npm pack using the same node and npm versions as we use to publish.

Are you thinking you want 2x sets of tests, one unit tests, the other doing these e2e-style tests against an npm install?

I thought about that but I wasn't sure how useful it'd be since the tests mostly look e2e-style already. Your call though, happy to split things out.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep it how it is then, I don't have strong opinions either way but it's great to know you thought about this.


describe('ts-node', function () {
const cmd = `node "${BIN_PATH}" --project "${PROJECT}"`
const cmd = `"${BIN_PATH}" --project "${PROJECT}"`

this.timeout(10000)

Expand All @@ -35,7 +47,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 +85,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 +420,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 +493,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
@@ -1 +1,3 @@
!node_modules/
!from-node-modules/node_modules/
package-lock.json
ts-node-packed.tgz
5 changes: 5 additions & 0 deletions tests/package.json
@@ -0,0 +1,5 @@
{
"dependencies": {
"ts-node": "file:ts-node-packed.tgz"
}
}