Skip to content

Commit

Permalink
Tests (#83)
Browse files Browse the repository at this point in the history
* 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
lalexdotcom authored Feb 5, 2023
1 parent 6105689 commit b0c461e
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 335 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
"lint": "eslint src test",
"test": "c8 --reporter=text --reporter=html tap --reporter=spec --no-coverage test/*.test.js && npm run test:types",
"test:index": "c8 --reporter=text --reporter=html tap --reporter=spec --no-coverage test/index.test.js && npm run test:types",
"test:dev": "c8 --reporter=text --reporter=html tap --reporter=spec --no-coverage test/development-mode.test.js && npm run test:types",
"test:cache": "c8 --reporter=text --reporter=html tap --reporter=spec --no-coverage test/cached-build.test.js && npm run test:types",
"test:clean-plugin": "c8 --reporter=text --reporter=html tap --reporter=spec --no-coverage test/clean-plugin.test.js && npm run test:types",
"test:dev-mode": "c8 --reporter=text --reporter=html tap --reporter=spec --no-coverage test/development-mode.test.js && npm run test:types",
"test:explicit": "c8 --reporter=text --reporter=html tap --reporter=spec --no-coverage test/explicit-import.test.js && npm run test:types",
"test:clean": "c8 --reporter=text --reporter=html tap --reporter=spec --no-coverage test/clean-plugin.test.js && npm run test:types",
"test:watch": "tap --watch --reporter=spec --no-browser --coverage-report=text --coverage-report=html test/*.test.js",
"test:ci": "c8 --reporter=text --reporter=json --check-coverage --branches 90 --functions 90 --lines 90 --statements 90 tap --no-color --no-coverage test/*.test.js && npm run test:types",
"test:types": "tsd",
Expand Down
120 changes: 51 additions & 69 deletions test/cached-build.test.js
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')
})
})
})
147 changes: 43 additions & 104 deletions test/clean-plugin.test.js
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')
})
})
})
43 changes: 14 additions & 29 deletions test/development-mode.test.js
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!')
})
})
Loading

0 comments on commit b0c461e

Please sign in to comment.