Files
pnpm/test/install/lifecycleScripts.ts
2017-06-28 20:11:40 +03:00

65 lines
2.3 KiB
TypeScript

import tape = require('tape')
import promisifyTape from 'tape-promise'
import {installPkgs, install} from '../../src'
import {
prepare,
testDefaults,
} from '../utils'
import path = require('path')
import loadJsonFile = require('load-json-file')
import rimraf = require('rimraf-then')
const pkgRoot = path.join(__dirname, '..', '..')
const pnpmPkg = loadJsonFile.sync(path.join(pkgRoot, 'package.json'))
const test = promisifyTape(tape)
test('run pre/postinstall scripts', async function (t) {
const project = prepare(t)
await installPkgs(['pre-and-postinstall-scripts-example'], testDefaults({saveDev: true}))
const generatedByPreinstall = project.requireModule('pre-and-postinstall-scripts-example/generated-by-preinstall')
t.ok(typeof generatedByPreinstall === 'function', 'generatedByPreinstall() is available')
const generatedByPostinstall = project.requireModule('pre-and-postinstall-scripts-example/generated-by-postinstall')
t.ok(typeof generatedByPostinstall === 'function', 'generatedByPostinstall() is available')
await rimraf('node_modules')
// testing that the packages are not installed even though they are in shrinkwrap
// and that their scripts are not tried to be executed
await install(testDefaults({production: true}))
{
const generatedByPreinstall = project.requireModule('pre-and-postinstall-scripts-example/generated-by-preinstall')
t.ok(typeof generatedByPreinstall === 'function', 'generatedByPreinstall() is not available')
const generatedByPostinstall = project.requireModule('pre-and-postinstall-scripts-example/generated-by-postinstall')
t.ok(typeof generatedByPostinstall === 'function', 'generatedByPostinstall() is not available')
}
})
test('run install scripts', async function (t) {
const project = prepare(t)
await installPkgs(['install-script-example'], testDefaults())
const generatedByInstall = project.requireModule('install-script-example/generated-by-install')
t.ok(typeof generatedByInstall === 'function', 'generatedByInstall() is available')
})
test('installation fails if lifecycle script fails', async (t: tape.Test) => {
const project = prepare(t, {
scripts: {
preinstall: 'exit 1'
},
})
try {
await install(testDefaults())
t.fail('should have failed')
} catch (err) {
t.equal(err['code'], 'ELIFECYCLE', 'failed with correct error code')
}
})