Skip to content

Commit

Permalink
Add a basic test.
Browse files Browse the repository at this point in the history
  • Loading branch information
francois2metz committed May 5, 2024
1 parent 6005912 commit e884a4e
Show file tree
Hide file tree
Showing 15 changed files with 4,057 additions and 1,419 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ jobs:

- run: yarn install --immutable
- run: yarn build
- run: yarn test
2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
nodeLinker: node-modules

nmHoistingLimits: workspaces
yarnPath: .yarn/releases/yarn-4.2.1.cjs
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"license": "ISC",
"devDependencies": {
"parcel": "^2.2.0",
"parcel-transformer-markdown-front-matter": "workspace:"
"parcel-transformer-markdown-front-matter": "workspace:^"
}
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"watch": "parcel watch",
"build": "parcel build",
"example": "yarn workspace example build",
"test": "rm -rf .parcel-cache && rm -rf dist && node test/index.cjs"
"test": "yarn workspace test test"
},
"keywords": [
"parcel",
Expand Down Expand Up @@ -52,7 +52,8 @@
"parcel": "^2.2.0"
},
"workspaces": [
"example"
"example/",
"test/"
],
"packageManager": "[email protected]"
}
6 changes: 6 additions & 0 deletions test/.parcelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "@parcel/config-default",
"transformers": {
"*.md": ["parcel-transformer-markdown-front-matter"]
}
}
17 changes: 0 additions & 17 deletions test/example/index.html

This file was deleted.

2 changes: 0 additions & 2 deletions test/example/index.js

This file was deleted.

3 changes: 0 additions & 3 deletions test/example/index.md

This file was deleted.

38 changes: 0 additions & 38 deletions test/index.cjs

This file was deleted.

44 changes: 44 additions & 0 deletions test/markdown.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const fs = require('node:fs/promises');
const url = require('node:url');
const { Parcel } = require('@parcel/core');
const jsdom = require('jsdom');
const { JSDOM } = jsdom;

const bundler = new Parcel({
entries: './markdown/index.html',
defaultConfig: '@parcel/config-default',
defaultTargetOptions: {
distDir: './dist'
},
shouldDisableCache: true,
});

class FileResourceLoader extends jsdom.ResourceLoader {
fetch(url, options) {
const parsedUrl = new URL(url);
return fs.readFile(`./dist${parsedUrl.pathname}`);
}
}

test('render a markdown file', async () => {
return expect(new Promise(async (resolve, reject) => {
await bundler.run();
const virtualConsole = new jsdom.VirtualConsole();
const logs = [];
virtualConsole.on('log', (message) => {
logs.push(message);
});
const dom = await JSDOM.fromFile('./dist/index.html', {
runScripts: 'dangerously',
resources: new FileResourceLoader(),
virtualConsole,
});
dom.window.onEnd = () => {
resolve(logs);
};
})).resolves.toEqual([
'parcel-transformer-markdown-front-matter',
`<h1 id="markdown">Markdown</h1>
<p>Markdown content</p>`
]);
});
11 changes: 11 additions & 0 deletions test/markdown/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<script type="module" src="./index.js"></script>
</head>
<body>
</body>
</html>
4 changes: 4 additions & 0 deletions test/markdown/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import md from './index.md';
console.log(md.title);
console.log(md.__content.trim());
window.onEnd();
7 changes: 7 additions & 0 deletions test/markdown/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: parcel-transformer-markdown-front-matter
---

# Markdown

Markdown content
16 changes: 16 additions & 0 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "test",
"version": "1.0.0",
"private": true,
"scripts": {
"test": "jest"
},
"license": "MIT",
"devDependencies": {
"@parcel/config-default": "^2.12.0",
"@parcel/core": "^2.12.0",
"jest": "^29.7.0",
"jsdom": "^24.0.0",
"parcel-transformer-markdown-front-matter": "workspace:^"
}
}
Loading

0 comments on commit e884a4e

Please sign in to comment.