@ava/typescript
Advanced tools
Comparing version 1.1.1 to 2.0.0
94
index.js
'use strict'; | ||
const path = require('path'); | ||
const escapeStringRegexp = require('escape-string-regexp'); | ||
const execa = require('execa'); | ||
const pkg = require('./package.json'); | ||
const help = `See https://github.com/avajs/typescript/blob/v${pkg.version}/README.md`; | ||
function isPlainObject(x) { | ||
@@ -12,21 +13,63 @@ return x !== null && typeof x === 'object' && Reflect.getPrototypeOf(x) === Object.prototype; | ||
function isValidExtensions(extensions) { | ||
return Array.isArray(extensions) && | ||
extensions.length > 0 && | ||
extensions.every(ext => typeof ext === 'string' && ext !== '') && | ||
new Set(extensions).size === extensions.length; | ||
} | ||
function validate(target, properties) { | ||
for (const key of Object.keys(properties)) { | ||
const {required, isValid} = properties[key]; | ||
const missing = !Reflect.has(target, key); | ||
function isValidRewritePaths(rewritePaths) { | ||
if (!isPlainObject(rewritePaths)) { | ||
return false; | ||
if (missing) { | ||
if (required) { | ||
throw new Error(`Missing '${key}' property in TypeScript configuration for AVA. ${help}`); | ||
} | ||
continue; | ||
} | ||
if (!isValid(target[key])) { | ||
throw new Error(`Invalid '${key}' property in TypeScript configuration for AVA. ${help}`); | ||
} | ||
} | ||
return Object.entries(rewritePaths).every(([from, to]) => { | ||
return from.endsWith('/') && typeof to === 'string' && to.endsWith('/'); | ||
}); | ||
for (const key of Object.keys(target)) { | ||
if (!Reflect.has(properties, key)) { | ||
throw new Error(`Unexpected '${key}' property in TypeScript configuration for AVA. ${help}`); | ||
} | ||
} | ||
} | ||
async function compileTypeScript(projectDir) { | ||
return execa('tsc', ['--incremental'], {preferLocal: true, cwd: projectDir}); | ||
} | ||
const configProperties = { | ||
compile: { | ||
required: true, | ||
isValid(compile) { | ||
return compile === false || compile === 'tsc'; | ||
} | ||
}, | ||
rewritePaths: { | ||
required: true, | ||
isValid(rewritePaths) { | ||
if (!isPlainObject(rewritePaths)) { | ||
return false; | ||
} | ||
return Object.entries(rewritePaths).every(([from, to]) => { | ||
return from.endsWith('/') && typeof to === 'string' && to.endsWith('/'); | ||
}); | ||
} | ||
}, | ||
extensions: { | ||
required: false, | ||
isValid(extensions) { | ||
return Array.isArray(extensions) && | ||
extensions.length > 0 && | ||
extensions.every(ext => typeof ext === 'string' && ext !== '') && | ||
new Set(extensions).size === extensions.length; | ||
} | ||
} | ||
}; | ||
module.exports = ({negotiateProtocol}) => { | ||
const protocol = negotiateProtocol(['ava-3.2', 'ava-3'], {version: pkg.version}); | ||
const protocol = negotiateProtocol(['ava-3.2'], {version: pkg.version}); | ||
if (protocol === null) { | ||
@@ -38,19 +81,12 @@ return; | ||
main({config}) { | ||
let valid = false; | ||
if (isPlainObject(config)) { | ||
const keys = Object.keys(config); | ||
if (keys.every(key => key === 'extensions' || key === 'rewritePaths')) { | ||
valid = | ||
(config.extensions === undefined || isValidExtensions(config.extensions)) && | ||
isValidRewritePaths(config.rewritePaths); | ||
} | ||
if (!isPlainObject(config)) { | ||
throw new Error(`Unexpected Typescript configuration for AVA. ${help}`); | ||
} | ||
if (!valid) { | ||
throw new Error(`Unexpected Typescript configuration for AVA. See https://github.com/avajs/typescript/blob/v${pkg.version}/README.md for allowed values.`); | ||
} | ||
validate(config, configProperties); | ||
const { | ||
extensions = ['ts'], | ||
rewritePaths: relativeRewritePaths | ||
rewritePaths: relativeRewritePaths, | ||
compile | ||
} = config; | ||
@@ -66,2 +102,6 @@ | ||
async compile() { | ||
if (compile === 'tsc') { | ||
await compileTypeScript(protocol.projectDir); | ||
} | ||
return { | ||
@@ -68,0 +108,0 @@ extensions: extensions.slice(), |
{ | ||
"name": "@ava/typescript", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "TypeScript provider for AVA", | ||
"engines": { | ||
"node": ">=10.18.0 <11 || >=12.14.0 <13 || >=13.5.0" | ||
"node": ">=12.22 <13 || >=14.16 <15 || >=15" | ||
}, | ||
@@ -19,14 +19,16 @@ "files": [ | ||
"scripts": { | ||
"test": "xo && nyc ava" | ||
"test": "xo && c8 ava" | ||
}, | ||
"dependencies": { | ||
"escape-string-regexp": "^2.0.0" | ||
"escape-string-regexp": "^4.0.0", | ||
"execa": "^5.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^3.0.0", | ||
"execa": "^4.0.0", | ||
"nyc": "^15.0.0", | ||
"xo": "^0.25.3" | ||
"ava": "^3.15.0", | ||
"c8": "^7.7.1", | ||
"del": "^6.0.0", | ||
"typescript": "^4.2.4", | ||
"xo": "^0.38.2" | ||
}, | ||
"nyc": { | ||
"c8": { | ||
"reporter": [ | ||
@@ -38,3 +40,12 @@ "html", | ||
}, | ||
"ava": { | ||
"files": [ | ||
"!test/broken-fixtures/**" | ||
], | ||
"timeout": "60s" | ||
}, | ||
"xo": { | ||
"ignores": [ | ||
"test/broken-fixtures" | ||
], | ||
"rules": { | ||
@@ -41,0 +52,0 @@ "import/order": "off" |
# @ava/typescript | ||
Adds rudimentary [TypeScript](https://www.typescriptlang.org/) support to [AVA](https://avajs.dev). | ||
Adds [TypeScript](https://www.typescriptlang.org/) support to [AVA](https://avajs.dev). | ||
@@ -27,3 +27,4 @@ This is designed to work for projects that precompile TypeScript. It allows AVA to load the compiled JavaScript, while configuring AVA to treat the TypeScript files as test files. | ||
"src/": "build/" | ||
} | ||
}, | ||
"compile": false | ||
} | ||
@@ -36,2 +37,4 @@ } | ||
You can enable compilation via the `compile` property. If `false`, AVA will assume you have already compiled your project. If set to `'tsc'`, AVA will run the TypeScript compiler before running your tests. This can be inefficient when using AVA in watch mode. | ||
Output files are expected to have the `.js` extension. | ||
@@ -38,0 +41,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7836
148
67
2
5
+ Addedexeca@^5.0.0
+ Addedcross-spawn@7.0.5(transitive)
+ Addedescape-string-regexp@4.0.0(transitive)
+ Addedexeca@5.1.1(transitive)
+ Addedget-stream@6.0.1(transitive)
+ Addedhuman-signals@2.1.0(transitive)
+ Addedis-stream@2.0.1(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedmerge-stream@2.0.0(transitive)
+ Addedmimic-fn@2.1.0(transitive)
+ Addednpm-run-path@4.0.1(transitive)
+ Addedonetime@5.1.2(transitive)
+ Addedpath-key@3.1.1(transitive)
+ Addedshebang-command@2.0.0(transitive)
+ Addedshebang-regex@3.0.0(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedstrip-final-newline@2.0.0(transitive)
+ Addedwhich@2.0.2(transitive)
- Removedescape-string-regexp@2.0.0(transitive)
Updatedescape-string-regexp@^4.0.0