-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Tests refactoring * Renamed 'testPlan' var to 'planEstimation' * Take constants out of the createTests function * Use ?? operator instead of ??= nodejs@14 compatibility * Update new test introduced in #59
- Loading branch information
1 parent
6105689
commit b0c461e
Showing
7 changed files
with
333 additions
and
335 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,69 @@ | ||
'use strict' | ||
|
||
const { readFileSync, readdirSync } = require('fs') | ||
const { resolve, join } = require('path') | ||
const { test } = require('tap') | ||
const webpack = require('webpack') | ||
const { banner, PinoWebpackPlugin } = require('../src/index') | ||
const execa = require('execa') | ||
|
||
function runBuild(distFolder, onDone) { | ||
webpack( | ||
{ | ||
context: resolve(__dirname, 'fixtures'), | ||
mode: 'production', | ||
target: 'node', | ||
entry: { | ||
first: './first.js', | ||
'abc/cde/second': './second.js' | ||
}, | ||
output: { | ||
path: distFolder, | ||
filename: '[name]-[contenthash].js' | ||
}, | ||
cache: { | ||
name: 'test', | ||
type: 'filesystem', | ||
cacheDirectory: join(distFolder, 'cache'), | ||
buildDependencies: { | ||
config: [__filename] | ||
} | ||
}, | ||
plugins: [new PinoWebpackPlugin({ transports: ['pino-pretty'] })], | ||
optimization: { | ||
minimize: false | ||
} | ||
}, | ||
onDone | ||
) | ||
} | ||
const { join } = require('path') | ||
const { createTests } = require('./test-utils') | ||
|
||
test('it should correctly generate all required pino files when the cache is enabled on Webpack', (t) => { | ||
t.plan(9) | ||
const { PinoWebpackPlugin } = require('../src/index') | ||
|
||
test('it should correctly generate all required pino files when the cache is enabled on Webpack', (t) => { | ||
const distFolder = t.testdir() | ||
|
||
runBuild(distFolder, (err) => { | ||
t.error(err) | ||
|
||
runBuild(distFolder, (err, stats) => { | ||
t.error(err) | ||
t.notOk(stats.hasErrors()) | ||
const webpackConfig = { | ||
entry: { | ||
first: './first.js', | ||
'abc/cde/second': './second.js' | ||
}, | ||
output: { | ||
filename: '[name]-[contenthash].js' | ||
}, | ||
cache: { | ||
name: 'test', | ||
type: 'filesystem', | ||
cacheDirectory: join(distFolder, 'cache'), | ||
buildDependencies: { | ||
config: [__filename] | ||
} | ||
}, | ||
plugins: [new PinoWebpackPlugin({ transports: ['pino-pretty'] })] | ||
} | ||
|
||
// Find all files in the folder | ||
const rootFiles = readdirSync(distFolder).filter((e) => e.endsWith('.js')) | ||
const nestedFiles = readdirSync(resolve(distFolder, 'abc/cde')).filter((e) => e.endsWith('.js')) | ||
const { planEstimation, runBuild, testJSFileExists, testDependencyFileHasFooter, testEntrypointFile } = createTests( | ||
t, | ||
webpackConfig, | ||
distFolder | ||
) | ||
|
||
const firstFile = rootFiles.find((e) => e.startsWith('first-')) | ||
const secondFile = nestedFiles.find((e) => e.startsWith('second-')) | ||
const pinoFile = rootFiles.find((e) => e.startsWith('pino-file-')) | ||
const pinoPretty = rootFiles.find((e) => e.startsWith('pino-pretty-')) | ||
t.plan(planEstimation + 2) | ||
|
||
// Check that the root file starts with the banner and has the right path to pino-file | ||
const firstContent = readFileSync(resolve(distFolder, firstFile), 'utf-8') | ||
t.ok(firstContent.startsWith(banner)) | ||
t.ok( | ||
firstContent.includes( | ||
`globalThis.__bundlerPathsOverrides = {'pino/file': pinoWebpackAbsolutePath('./${pinoFile}')` | ||
) | ||
) | ||
runBuild(() => { | ||
console.log('Launch second build') | ||
runBuild(() => { | ||
const firstFile = testJSFileExists('first-') | ||
const secondFile = testJSFileExists('second-', 'abc/cde') | ||
|
||
execa(process.argv[0], [resolve(distFolder, firstFile)]).then(({ stdout }) => { | ||
t.match(stdout, /This is first!/) | ||
}) | ||
const threadStream = testJSFileExists('thread-stream-') | ||
const pinoWorker = testJSFileExists('pino-worker-') | ||
const pinoPipelineWorker = testJSFileExists('pino-pipeline-worker-') | ||
const pinoFile = testJSFileExists('pino-file-') | ||
const pinoPretty = testJSFileExists('pino-pretty-') | ||
|
||
const secondDistFilePath = resolve(distFolder, `abc/cde/${secondFile}`) | ||
const dependencies = { | ||
'pino/file': pinoFile, | ||
'thread-stream-worker': threadStream, | ||
'pino-worker': pinoWorker, | ||
'pino-pipeline-worker': pinoPipelineWorker, | ||
'pino-pretty': pinoPretty | ||
} | ||
|
||
// Check that the root file starts with the banner and has the right path to pino-file | ||
const secondContent = readFileSync(secondDistFilePath, 'utf-8') | ||
t.ok(secondContent.startsWith(banner)) | ||
t.ok(secondContent.includes(`'pino-pretty': pinoWebpackAbsolutePath('../../${pinoPretty}')`)) | ||
// Check that generated pino related files have the right footer | ||
for (const file of Object.values(dependencies)) { | ||
testDependencyFileHasFooter(file) | ||
} | ||
|
||
execa(process.argv[0], [secondDistFilePath]).then(({ stdout }) => { | ||
t.match(stdout, /This is second!/) | ||
}) | ||
testEntrypointFile(firstFile, dependencies, 'This is first') | ||
testEntrypointFile(secondFile, dependencies, 'This is second') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,119 +1,58 @@ | ||
'use strict' | ||
|
||
const { readFileSync, readdirSync } = require('fs') | ||
const { resolve } = require('path') | ||
const { test } = require('tap') | ||
const webpack = require('webpack') | ||
const { banner, footer, PinoWebpackPlugin } = require('../src/index') | ||
const execa = require('execa') | ||
const { CleanWebpackPlugin } = require('clean-webpack-plugin') | ||
|
||
function runBuild(distFolder, onDone) { | ||
webpack( | ||
{ | ||
context: resolve(__dirname, 'fixtures'), | ||
mode: 'production', | ||
target: 'node', | ||
entry: { | ||
first: './first.js', | ||
'abc/cde/second': './second.js' | ||
}, | ||
output: { | ||
path: distFolder, | ||
filename: '[name]-[contenthash].js' | ||
}, | ||
plugins: [new CleanWebpackPlugin(), new PinoWebpackPlugin({ transports: ['pino-pretty'] })], | ||
optimization: { | ||
minimize: false | ||
} | ||
}, | ||
onDone | ||
) | ||
} | ||
|
||
test('it should correctly generate all required pino files when the cache is enabled on Webpack', (t) => { | ||
t.plan(29) | ||
|
||
const distFolder = t.testdir() | ||
|
||
runBuild(distFolder, (err, stats) => { | ||
t.error(err) | ||
t.notOk(stats.hasErrors()) | ||
|
||
// Find all files in the folder | ||
const rootFiles = readdirSync(distFolder).filter((e) => e.endsWith('.js')) | ||
const nestedFiles = readdirSync(resolve(distFolder, 'abc/cde')).filter((e) => e.endsWith('.js')) | ||
|
||
const firstFile = rootFiles.find((e) => e.startsWith('first-')) | ||
const secondFile = nestedFiles.find((e) => e.startsWith('second-')) | ||
const threadStream = rootFiles.find((e) => e.startsWith('thread-stream-')) | ||
const pinoWorker = rootFiles.find((e) => e.startsWith('pino-worker-')) | ||
const pinoPipelineWorker = rootFiles.find((e) => e.startsWith('pino-pipeline-worker-')) | ||
const pinoFile = rootFiles.find((e) => e.startsWith('pino-file-')) | ||
const pinoPretty = rootFiles.find((e) => e.startsWith('pino-pretty-')) | ||
|
||
// Check that all required files have been generated | ||
t.ok(firstFile) | ||
t.ok(secondFile) | ||
t.ok(threadStream) | ||
t.ok(pinoWorker) | ||
t.ok(pinoPipelineWorker) | ||
t.ok(pinoFile) | ||
t.ok(pinoPretty) | ||
const { createTests } = require('./test-utils') | ||
|
||
runBuild(distFolder, (err, stats) => { | ||
t.error(err) | ||
t.notOk(stats.hasErrors()) | ||
const { PinoWebpackPlugin } = require('../src/index') | ||
const { CleanWebpackPlugin } = require('clean-webpack-plugin') | ||
|
||
// Find all files in the folder | ||
const rootFiles = readdirSync(distFolder).filter((e) => e.endsWith('.js')) | ||
const nestedFiles = readdirSync(resolve(distFolder, 'abc/cde')).filter((e) => e.endsWith('.js')) | ||
test('it should correctly generate all required pino files when the cache is enabled on Webpack', (t) => { | ||
const webpackConfig = { | ||
entry: { | ||
first: './first.js', | ||
'abc/cde/second': './second.js' | ||
}, | ||
output: { | ||
filename: '[name]-[contenthash].js' | ||
}, | ||
plugins: [new CleanWebpackPlugin(), new PinoWebpackPlugin({ transports: ['pino-pretty'] })] | ||
} | ||
|
||
const firstFile = rootFiles.find((e) => e.startsWith('first-')) | ||
const secondFile = nestedFiles.find((e) => e.startsWith('second-')) | ||
const threadStream = rootFiles.find((e) => e.startsWith('thread-stream-')) | ||
const pinoWorker = rootFiles.find((e) => e.startsWith('pino-worker-')) | ||
const pinoPipelineWorker = rootFiles.find((e) => e.startsWith('pino-pipeline-worker-')) | ||
const pinoFile = rootFiles.find((e) => e.startsWith('pino-file-')) | ||
const pinoPretty = rootFiles.find((e) => e.startsWith('pino-pretty-')) | ||
const { planEstimation, runBuild, testJSFileExists, testDependencyFileHasFooter, testEntrypointFile } = createTests( | ||
t, | ||
webpackConfig | ||
) | ||
|
||
// Check that all required files have been generated | ||
t.ok(firstFile) | ||
t.ok(secondFile) | ||
t.ok(threadStream) | ||
t.ok(pinoWorker) | ||
t.ok(pinoPipelineWorker) | ||
t.ok(pinoFile) | ||
t.ok(pinoPretty) | ||
t.plan(planEstimation + 2) | ||
|
||
runBuild(() => { | ||
console.log('Launch second build') | ||
runBuild(() => { | ||
const firstFile = testJSFileExists('first-') | ||
const secondFile = testJSFileExists('second-', 'abc/cde') | ||
|
||
const threadStream = testJSFileExists('thread-stream-') | ||
const pinoWorker = testJSFileExists('pino-worker-') | ||
const pinoPipelineWorker = testJSFileExists('pino-pipeline-worker-') | ||
const pinoFile = testJSFileExists('pino-file-') | ||
const pinoPretty = testJSFileExists('pino-pretty-') | ||
|
||
const dependencies = { | ||
'pino/file': pinoFile, | ||
'thread-stream-worker': threadStream, | ||
'pino-worker': pinoWorker, | ||
'pino-pipeline-worker': pinoPipelineWorker, | ||
'pino-pretty': pinoPretty | ||
} | ||
|
||
// Check that generated pino related files have the right footer | ||
for (const file of [threadStream, pinoWorker, pinoPipelineWorker, pinoFile, pinoPretty]) { | ||
t.ok(readFileSync(resolve(distFolder, file), 'utf-8').endsWith(footer)) | ||
for (const file of Object.values(dependencies)) { | ||
testDependencyFileHasFooter(file) | ||
} | ||
|
||
// Check that the root file starts with the banner and has the right path to pino-file | ||
const firstContent = readFileSync(resolve(distFolder, firstFile), 'utf-8') | ||
t.ok(firstContent.startsWith(banner)) | ||
t.ok( | ||
firstContent.includes( | ||
`globalThis.__bundlerPathsOverrides = {'pino/file': pinoWebpackAbsolutePath('./${pinoFile}')` | ||
) | ||
) | ||
|
||
execa(process.argv[0], [resolve(distFolder, firstFile)]).then(({ stdout }) => { | ||
t.match(stdout, /This is first!/) | ||
}) | ||
|
||
const secondDistFilePath = resolve(distFolder, `abc/cde/${secondFile}`) | ||
|
||
// Check that the root file starts with the banner and has the right path to pino-file | ||
const secondContent = readFileSync(secondDistFilePath, 'utf-8') | ||
t.ok(secondContent.startsWith(banner)) | ||
t.ok(secondContent.includes(`'pino-pretty': pinoWebpackAbsolutePath('../../${pinoPretty}')`)) | ||
|
||
execa(process.argv[0], [resolve(secondDistFilePath)]).then(({ stdout }) => { | ||
t.match(stdout, /This is second!/) | ||
}) | ||
testEntrypointFile(firstFile, dependencies, 'This is first') | ||
testEntrypointFile(secondFile, dependencies, 'This is second') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,25 @@ | ||
'use strict' | ||
|
||
const { resolve } = require('path') | ||
const { test } = require('tap') | ||
const webpack = require('webpack') | ||
const { PinoWebpackPlugin } = require('../src/index') | ||
const execa = require('execa') | ||
|
||
function runBuild(distFolder, onDone) { | ||
webpack( | ||
{ | ||
context: resolve(__dirname, 'fixtures'), | ||
mode: 'development', | ||
target: 'node', | ||
entry: { | ||
main: './second.js' | ||
}, | ||
output: { | ||
path: distFolder | ||
}, | ||
plugins: [new PinoWebpackPlugin({ transports: ['pino-pretty'] })] | ||
}, | ||
onDone | ||
) | ||
} | ||
const { createTests } = require('./test-utils') | ||
|
||
const { PinoWebpackPlugin } = require('../src/index') | ||
|
||
test('should work in webpack development mode', (t) => { | ||
t.plan(3) | ||
const webpackConfig = { | ||
mode: 'development', | ||
entry: { | ||
main: './second.js' | ||
}, | ||
plugins: [new PinoWebpackPlugin({ transports: ['pino-pretty'] })] | ||
} | ||
|
||
const distFolder = t.testdir() | ||
const { runBuild, testEntrypointFile } = createTests(t, webpackConfig) | ||
|
||
runBuild(distFolder, (err, stats) => { | ||
t.error(err) | ||
t.notOk(stats.hasErrors()) | ||
t.plan(4) | ||
|
||
execa(process.argv[0], [resolve(distFolder, 'main.js')]).then(({ stdout }) => { | ||
t.match(stdout, /This is second!/) | ||
}) | ||
runBuild(() => { | ||
testEntrypointFile('main.js', {}, 'This is second!') | ||
}) | ||
}) |
Oops, something went wrong.