Skip to content

Commit

Permalink
test: add .cjs test script & fix test issues
Browse files Browse the repository at this point in the history
  • Loading branch information
webketje committed Nov 2, 2023
1 parent 0e456e2 commit 4b867fb
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
lib
lib
test/index.cjs
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ npm-debug.log*
coverage
coverage.info
test/fixtures/*/build
test/index.cjs
node_modules
lib/*.map
lib/*.cjs
Expand Down
4 changes: 2 additions & 2 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Plugin } from 'metalsmith';
export default initializeInPlace;
export default inPlace;
export type Render = (source: string, options: any, locals: any) => string;
export type RenderAsync = (source: string, options: any, locals: any, callback: Function) => Promise<string>;
export type Compile = (source: string, options: any) => string;
Expand Down Expand Up @@ -54,4 +54,4 @@ export type Options = {
* }
* }}))
*/
declare function initializeInPlace(options?: Options): Plugin;
declare function inPlace(options?: Options): Plugin;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"format": "prettier --write \"**/*.{yml,md,js,json}\"",
"format:check": "prettier --list-different \"**/*.{yml,md,js,json}\"",
"test": "c8 mocha",
"test:cjs": "microbundle -i test/index.js -o test/index.cjs -f cjs --target=node --pkg-main=false --no-sourcemap --external assert-dir-equal,metalsmith,jstransformer-pug --generateTypes=false && mocha test/index.cjs",
"release": "release-it",
"build": "microbundle --target node -f cjs,esm --strict --generateTypes=false",
"prepack": "npm run build",
Expand Down
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,12 @@ function inPlace(options = {}) {
// Let the user know when there are no files to process
if (validFiles.length === 0) {
debug.warn('No valid files to process.')
return done()
} else {
debug('Rendering %s files', validFiles.length)
done()
return
}

debug('Rendering %s files', validFiles.length)

// Map all files that should be processed to an array of promises and call done when finished
return Promise.all(validFiles.map((filename) => render({ filename, files, metalsmith, options, transform })))
.then(() => done())
Expand Down
24 changes: 11 additions & 13 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ describe('@metalsmith/in-place', () => {
})
})

it('should throw on unresolved transform option', (done) => {
Promise.allSettled([
it('should throw on unresolved transform option', () => {
return Promise.allSettled([
Metalsmith(fixture('transform-option'))
.env('DEBUG', process.env.DEBUG)
.use(plugin({ transform: 'invalid' }))
Expand All @@ -115,17 +115,15 @@ describe('@metalsmith/in-place', () => {
.use(plugin({ transform: 'jstransformer-invalid' }))
.process()
]).then((promises) => {
for (let i = 0; i < 3; i++) {
const reason = promises[i].reason
try {
strictEqual(reason instanceof Error, true)
strictEqual(reason.code, 'ERR_MODULE_NOT_FOUND')
} catch (err) {
done(err)
break
}
}
done()
deepStrictEqual(
promises.map((p) => p.status),
['rejected', 'rejected', 'rejected']
)
// in ESM, this yields ERR_MODULE_NOT_FOUND, but in CJS the ERR_ prefix is omitted
deepStrictEqual(
promises.map((p) => p.reason.code.replace('ERR_', '')),
['MODULE_NOT_FOUND', 'MODULE_NOT_FOUND', 'MODULE_NOT_FOUND']
)
})
})

Expand Down

0 comments on commit 4b867fb

Please sign in to comment.