From 54952ddd0e8a2bac1095fc295041b10f3a9daeba Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Sat, 21 Jan 2023 07:29:26 +0100 Subject: [PATCH] Add tests (#59) A GitHub actions workflow file was added that runs the tests for each pull request, or push to main or tags. --- .github/workflows/ci.yaml | 21 ++++++++++++ example/basic/webpack.config.js | 3 +- example/external-config/webpack.config.js | 3 +- example/svgo-error/webpack.config.js | 3 +- index.test.js | 42 +++++++++++++++++++++++ package.json | 2 +- 6 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/ci.yaml create mode 100644 index.test.js diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..f91ff9b --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,21 @@ +name: test + +on: + pull_request: + push: + branches: [main] + tags: ['*'] + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + node-version: [16, 18] + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + - run: npm ci + - run: npm test diff --git a/example/basic/webpack.config.js b/example/basic/webpack.config.js index 716c9ec..60dea22 100644 --- a/example/basic/webpack.config.js +++ b/example/basic/webpack.config.js @@ -4,7 +4,8 @@ module.exports = { entry: "./entry", output: { path: __dirname + "/dist", - filename: "bundle.js" + filename: "bundle.js", + assetModuleFilename: '[name][ext]' }, module: { rules: [ diff --git a/example/external-config/webpack.config.js b/example/external-config/webpack.config.js index 1133889..717dcdf 100644 --- a/example/external-config/webpack.config.js +++ b/example/external-config/webpack.config.js @@ -4,7 +4,8 @@ module.exports = { entry: "./entry", output: { path: __dirname + "/dist", - filename: "bundle.js" + filename: "bundle.js", + assetModuleFilename: "[name][ext]" }, module: { rules: [ diff --git a/example/svgo-error/webpack.config.js b/example/svgo-error/webpack.config.js index 7d1079d..eabc20d 100644 --- a/example/svgo-error/webpack.config.js +++ b/example/svgo-error/webpack.config.js @@ -4,7 +4,8 @@ module.exports = { entry: "./entry", output: { path: __dirname + "/dist", - filename: "bundle.js" + filename: "bundle.js", + assetModuleFilename: "[name][ext]" }, module: { rules: [ diff --git a/index.test.js b/index.test.js new file mode 100644 index 0000000..f4ff0df --- /dev/null +++ b/index.test.js @@ -0,0 +1,42 @@ +const assert = require('node:assert/strict'); +const fs = require('node:fs/promises'); +const path = require('node:path'); +const { test } = require('node:test'); + +const webpack = require('webpack'); + +const basic = require('./example/basic/webpack.config.js'); +const externalConfig = require('./example/external-config/webpack.config.js'); +const svgoError = require('./example/svgo-error/webpack.config.js'); + +function buildWebpack(config) { + const compiler = webpack(config); + return new Promise((resolve, reject) => { + compiler.run((error, result) => { + if(error) { + reject(error); + } else { + resolve(result); + } + }); + }); +} + +test('basic', async () => { + const result = await buildWebpack(basic); + assert.equal(result.hasErrors(), false); + const output = await fs.readFile(path.join(__dirname, 'example', 'basic', 'dist', 'SVG_logo.svg'), 'utf8'); + assert.equal(output, ''); +}); + +test('external-config', async () => { + const result = await buildWebpack(externalConfig); + assert.equal(result.hasErrors(), false); + const output = await fs.readFile(path.join(__dirname, 'example', 'external-config', 'dist', 'SVG_logo.svg'), 'utf8'); + assert.equal(output, 'SVG LogoSVG'); +}); + +test('svgo-error', async () => { + const result = await buildWebpack(svgoError); + assert.equal(result.hasErrors(), true); +}); diff --git a/package.json b/package.json index 3cc66ed..8c33c91 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "svgo loader for webpack", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "node --test" }, "repository": { "type": "git",