Miel Truyen 52c104f781
Some checks failed
continuous-integration/drone/push Build is failing
0.0.3: Fixed a bug that showed up in watch mode
2023-05-20 21:05:37 +02:00

98 lines
2.4 KiB
JavaScript

import {join, dirname} from "node:path";
import test from "ava";
import * as rollup from "rollup";
import {debugPrintOutput, getCode} from "../util/test.js";
import {resolve} from "node:path";
import {writeFile} from "node:fs/promises";
import html from "../../src/index.ts";
const output = {
dir: 'output', // Output all files
format: 'es', // iifi and cjs should be added to tests
sourcemap: true,// Test if #sourcemapUrl is not accidentally included in the html-output
};
import {fileURLToPath} from "node:url";
import {pathToFileURL} from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
process.chdir(join(__dirname, 'fixtures'));
test.serial('watch', async (t) => {
const origContent = `
export const a = 1; // DO NOT CHANGE ME HERE, but in ../test.js
`;
const changeContent = `
export const a = 2; // If i show up as a changed file, then the watch test has gone wrong!
`
const path = resolve(__dirname, 'fixtures/watched-file.js');
await writeFile(path, origContent, {encoding: 'utf-8'});
const watcher = rollup.watch({
input: 'index.html',
output,
plugins: [
html({
}),
],
watch: {
skipWrite: true,
}
});
const steps = [
async (bundle)=>{
await writeFile(path, changeContent, {encoding: 'utf-8'});
// Just wait on the watch mode to pick up on the changes
},
async (bundle)=>{
const code = await getCode(bundle, output, true);
debugPrintOutput('watch',code);
// Reset the source file
await writeFile(path, origContent, {encoding: 'utf-8'});
// Assert the output is what we exapect;
t.snapshot(code);
watcher
},
];
await new Promise((resolve, reject)=>{
watcher.on('event', async (event) => {
const {result} = event;
switch (event.code) {
case "START":
t.log(`WATCH STARTED`);
break;
case "BUNDLE_START":
t.log(`REBUILDING...`);
break;
case "BUNDLE_END":
t.log(`Rebuilt...`);
const cb = steps.shift();
const generated = await result.generate(output);
const cbResult = await cb(result);
if(steps.length===0){
watcher.close();
resolve();
}
break;
case "ERROR":
reject(event.error);
break;
}
if (result) {
result.close();
}
});
});
});