Skip to content

Commit

Permalink
Added mocked tests
Browse files Browse the repository at this point in the history
  • Loading branch information
neogeek committed May 15, 2024
1 parent 95dcd65 commit 182c9aa
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 5 deletions.
110 changes: 108 additions & 2 deletions src/build.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import test, { describe } from 'node:test';
/* node:coverage disable */

import test, { describe, it, mock } from 'node:test';
import assert from 'node:assert';
import fs from 'node:fs/promises';

import { buildFile, calculateOutputPathFromInputPath } from './build.js';
import {
buildFile,
buildFiles,
calculateOutputPathFromInputPath
} from './build.js';

describe('build files', async () => {
test('calculate output path from input path', () => {
Expand All @@ -24,6 +31,17 @@ describe('build files', async () => {
}
);
});
test('build multiple files that returns a string', async () => {
assert.deepEqual(
await buildFiles(['./tests/mocks/export-returns-string.mjs']),
[
{
path: './tests/mocks/export-returns-string.mjs',
contents: '<h1>Hello, string!</h1>'
}
]
);
});
test('build file that returns a function', async () => {
assert.deepEqual(
await buildFile('./tests/mocks/export-returns-function.mjs'),
Expand All @@ -33,6 +51,12 @@ describe('build files', async () => {
}
);
});
test('build multiple files that returns a function', async () => {
assert.deepEqual(
await buildFiles(['./tests/mocks/export-returns-function.mjs']),
[]
);
});
test("build file that doesn't return anything", async () => {
assert.deepEqual(
await buildFile('./tests/mocks/export-returns-nothing.mjs'),
Expand All @@ -42,4 +66,86 @@ describe('build files', async () => {
}
);
});
test("build multiple files that don't return anything", async () => {
assert.deepEqual(
await buildFiles(['./tests/mocks/export-returns-nothing.mjs']),
[]
);
});
test('build file that returns different things', async () => {
assert.deepEqual(
await buildFiles([
'./tests/mocks/export-returns-string.mjs',
'./tests/mocks/export-returns-function.mjs',
'./tests/mocks/export-returns-nothing.mjs'
]),
[
{
path: './tests/mocks/export-returns-string.mjs',
contents: '<h1>Hello, string!</h1>'
}
]
);
});
test('write file (and make directory as needed)', async () => {
const tmp = {};

mock.method(fs, 'mkdir', async () => {});
mock.method(fs, 'readFile', async path => tmp[path]);
mock.method(
fs,
'writeFile',
async (path, contents) => (tmp[path] = contents)
);

const { writeFileAndMakeDir } = await import('./build.js');

await writeFileAndMakeDir('build/index.html', '<h1>Hello, world!</h1>');

assert.equal(
await fs.readFile('build/index.html'),
'<h1>Hello, world!</h1>'
);

mock.reset();
});
test('write multiple files (and make directory as needed)', async () => {
const tmp = {};

mock.method(fs, 'mkdir', async () => {});
mock.method(fs, 'readFile', async path => tmp[path]);
mock.method(
fs,
'writeFile',
async (path, contents) => (tmp[path] = contents)
);

const { writeFiles } = await import('./build.js');

await writeFiles(
[
{
path: 'index.html',
contents: '<h1>Hello, world!</h1>'
},
{
path: 'about.html',
contents: '<h1>Hello, world!</h1>'
}
],
'build/'
);

assert.equal(
await fs.readFile('build/index.html'),
'<h1>Hello, world!</h1>'
);

assert.equal(
await fs.readFile('build/about/index.html'),
'<h1>Hello, world!</h1>'
);

mock.reset();
});
});
6 changes: 3 additions & 3 deletions src/build.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { writeFile, mkdir } from 'node:fs/promises';
import fs from 'node:fs/promises';
import { dirname, join, parse, resolve } from 'node:path';

/**
Expand Down Expand Up @@ -50,9 +50,9 @@ export const buildFiles = async (
* @param {string} contents
*/
export const writeFileAndMakeDir = async (path: string, contents: string) => {
await mkdir(dirname(path), { recursive: true });
await fs.mkdir(dirname(path), { recursive: true });

await writeFile(path, contents);
await fs.writeFile(path, contents);
};

/**
Expand Down
2 changes: 2 additions & 0 deletions src/html.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* node:coverage disable */

import test, { describe } from 'node:test';
import assert from 'node:assert';

Expand Down

0 comments on commit 182c9aa

Please sign in to comment.