Skip to content

Commit

Permalink
Add tests for snowpack babel plugin. (#162)
Browse files Browse the repository at this point in the history
* Rename package to packageName

* Add tests for babel-plugin

* Update package.json scripts

* Use fixtures instead of own custom test data

* Refer mock dep instead of real hyperapp dep

Co-authored-by: Fred K. Schott <fkschott@gmail.com>
  • Loading branch information
yukukotani and FredKSchott committed Jan 19, 2020
1 parent 64453a2 commit 1172ad0
Show file tree
Hide file tree
Showing 19 changed files with 80 additions and 7 deletions.
1 change: 1 addition & 0 deletions __tests__/babel-plugin/__fixtures__/addVersion/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import module from 'example-dep';
3 changes: 3 additions & 0 deletions __tests__/babel-plugin/__fixtures__/addVersion/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"addVersion": true
}
1 change: 1 addition & 0 deletions __tests__/babel-plugin/__fixtures__/addVersion/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import module from '/web_modules/example-dep.js?v=1.2.3';
1 change: 1 addition & 0 deletions __tests__/babel-plugin/__fixtures__/dir/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import module from 'example-dep';
3 changes: 3 additions & 0 deletions __tests__/babel-plugin/__fixtures__/dir/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dir": "test_modules"
}
1 change: 1 addition & 0 deletions __tests__/babel-plugin/__fixtures__/dir/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import module from '/test_modules/example-dep.js';
1 change: 1 addition & 0 deletions __tests__/babel-plugin/__fixtures__/noOption/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import module from 'example-dep';
2 changes: 2 additions & 0 deletions __tests__/babel-plugin/__fixtures__/noOption/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
1 change: 1 addition & 0 deletions __tests__/babel-plugin/__fixtures__/noOption/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import module from '/web_modules/example-dep.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import module from 'any/local/file';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"optionalExtensions": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import module from '/web_modules/any/local/file.js';
6 changes: 6 additions & 0 deletions __tests__/babel-plugin/node_modules/example-dep/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
Empty file.
11 changes: 11 additions & 0 deletions __tests__/babel-plugin/runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const path = require('path');
const pluginTester = require('babel-plugin-tester').default;
const babelPlugin = require('../../assets/babel-plugin.js');

process.chdir(__dirname);

pluginTester({
plugin: babelPlugin,
pluginName: 'snowpack/assets/babel-plugin.js',
fixtures: path.join(__dirname, '__fixtures__'),
});
4 changes: 2 additions & 2 deletions assets/babel-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ function getWebDependencyName(dep) {
return dep.replace(/\.js$/, '');
}

function getPackageVersion(package) {
function getPackageVersion(packageName) {
// The root module for require.resolve is @babel/cli in default, so configure here.
const modulesPath = process.cwd() + '/node_modules';
const entryPointPath = require.resolve(package, {paths: [modulesPath]});
const entryPointPath = require.resolve(packageName, {paths: [modulesPath]});
const json = finder(entryPointPath).next().value;
return json.version;
}
Expand Down
34 changes: 34 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"lint": "prettier --check 'src/**/*.ts'",
"publish": "pika publish",
"version": "npm run build",
"test": "jest __tests__/integration/runner.js --test-timeout=15000",
"test": "npm run test:integration && npm run test:babel-plugin",
"test:integration": "jest __tests__/integration/runner.js --test-timeout=15000",
"test:babel-plugin": "jest __tests__/babel-plugin/runner.js",
"docs:build": "cat docs/* > www/index.md && npx @11ty/eleventy --config www/.eleventy.js",
"docs:watch": "cat docs/* > www/index.md && npx @11ty/eleventy --config www/.eleventy.js --serve",
"docs:deploy": "npm run docs:build && cd www/dist && now --prod"
Expand Down Expand Up @@ -66,19 +68,20 @@
"yargs-parser": "^16.1.0"
},
"devDependencies": {
"@11ty/eleventy-plugin-syntaxhighlight": "^3.0.1",
"@11ty/eleventy": "^0.10.0",
"eleventy-plugin-nesting-toc": "^1.1.0",
"markdown-it": "^10.0.0",
"markdown-it-anchor": "^5.2.5",
"@11ty/eleventy-plugin-syntaxhighlight": "^3.0.1",
"@pika/pack": "^0.5.0",
"@pika/plugin-build-node": "^0.8.1",
"@pika/plugin-copy-assets": "^0.8.1",
"@pika/plugin-simple-bin": "^0.8.1",
"@pika/plugin-ts-standard-pkg": "^0.8.1",
"@types/babel__traverse": "^7.0.7",
"@types/mkdirp": "^0.5.2",
"babel-plugin-tester": "^8.0.1",
"eleventy-plugin-nesting-toc": "^1.1.0",
"jest": "^24.9.0",
"markdown-it": "^10.0.0",
"markdown-it-anchor": "^5.2.5",
"prettier": "^1.18.2",
"typescript": "^3.6.4"
}
Expand Down

0 comments on commit 1172ad0

Please sign in to comment.