Skip to content

Commit

Permalink
Add expect.assertions()'s to guarantee checks are made
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmire committed Oct 17, 2023
1 parent 5e31cc8 commit 0f0417f
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const { withinSandbox } = createSandbox('utils');
describe('fs', () => {
describe('readFile', () => {
it('reads the contents of the given file as a UTF-8-encoded string', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const filePath = path.join(sandbox.directoryPath, 'test.file');

Expand All @@ -31,6 +33,8 @@ describe('fs', () => {
});

it('re-throws a wrapped version of any error that occurs, assigning it the same code and giving it a stack', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const filePath = path.join(sandbox.directoryPath, 'nonexistent.file');

Expand All @@ -51,6 +55,8 @@ describe('fs', () => {

describe('writeFile', () => {
it('writes the given data to the given file', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const filePath = path.join(sandbox.directoryPath, 'test.file');

Expand All @@ -63,6 +69,8 @@ describe('fs', () => {
});

it('re-throws a wrapped version of any error that occurs, assigning it the same code and giving it a stack', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
// Make sandbox root directory non-readable
await fs.promises.chmod(sandbox.directoryPath, 0o600);
Expand All @@ -85,6 +93,8 @@ describe('fs', () => {
describe('readJsonFile', () => {
describe('not given a custom parser', () => {
it('reads the contents of the given file as a UTF-8-encoded string and parses it using the JSON module', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const filePath = path.join(sandbox.directoryPath, 'test.json');

Expand All @@ -95,6 +105,8 @@ describe('fs', () => {
});

it('re-throws a wrapped version of any error that occurs, assigning it the same code and giving it a stack', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const filePath = path.join(sandbox.directoryPath, 'nonexistent.json');

Expand All @@ -115,6 +127,8 @@ describe('fs', () => {

describe('given a custom parser', () => {
it('reads the contents of the given file as a UTF-8-encoded string and parses it using the custom parser', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const filePath = path.join(sandbox.directoryPath, 'test.json');
const parser = {
Expand All @@ -132,6 +146,8 @@ describe('fs', () => {
});

it('re-throws a wrapped version of any error that occurs, assigning it the same code and giving it a stack', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const filePath = path.join(sandbox.directoryPath, 'nonexistent.json');
const parser = {
Expand Down Expand Up @@ -159,6 +175,8 @@ describe('fs', () => {
describe('writeJsonFile', () => {
describe('not given a custom stringifier', () => {
it('writes the given data to the given file as JSON (not reformatting it by default)', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const filePath = path.join(sandbox.directoryPath, 'test.json');

Expand All @@ -171,6 +189,8 @@ describe('fs', () => {
});

it('writes the given data to the given file as JSON (not reformatting it if "prettify" is false)', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const filePath = path.join(sandbox.directoryPath, 'test.json');

Expand All @@ -183,6 +203,8 @@ describe('fs', () => {
});

it('writes the given data to the given file as JSON (reformatting it if "prettify" is true)', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const filePath = path.join(sandbox.directoryPath, 'test.json');

Expand All @@ -195,6 +217,8 @@ describe('fs', () => {
});

it('re-throws a wrapped version of any error that occurs, assigning it the same code and giving it a stack', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
// Make sandbox root directory non-readable
await fs.promises.chmod(sandbox.directoryPath, 0o600);
Expand All @@ -218,6 +242,8 @@ describe('fs', () => {

describe('given a custom stringifier', () => {
it('writes the given data to the given file as JSON, using the stringifier (not reformatting it by default)', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const filePath = path.join(sandbox.directoryPath, 'test.json');
const stringifier = {
Expand Down Expand Up @@ -247,6 +273,8 @@ describe('fs', () => {
});

it('writes the given data to the given file as JSON (not reformatting it if "prettify" is false)', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const filePath = path.join(sandbox.directoryPath, 'test.json');
const stringifier = {
Expand Down Expand Up @@ -280,6 +308,8 @@ describe('fs', () => {
});

it('writes the given data to the given file as JSON (reformatting it if "prettify" is true)', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const filePath = path.join(sandbox.directoryPath, 'test.json');
const stringifier = {
Expand Down Expand Up @@ -313,6 +343,8 @@ describe('fs', () => {
});

it('re-throws a wrapped version of any error that occurs, assigning it the same code and giving it a stack', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
// Make sandbox root directory non-readable
await fs.promises.chmod(sandbox.directoryPath, 0o600);
Expand Down Expand Up @@ -354,6 +386,8 @@ describe('fs', () => {

describe('fileExists', () => {
it('returns true if the given path refers to an existing file', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const filePath = path.join(sandbox.directoryPath, 'test.file');
await fs.promises.writeFile(filePath, 'some content');
Expand All @@ -363,6 +397,8 @@ describe('fs', () => {
});

it('returns false if the given path refers to something that is not a file', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const directoryPath = path.join(
sandbox.directoryPath,
Expand All @@ -375,6 +411,8 @@ describe('fs', () => {
});

it('returns false if the given path does not refer to any existing entry', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const filePath = path.join(sandbox.directoryPath, 'nonexistent-entry');

Expand Down Expand Up @@ -404,6 +442,8 @@ describe('fs', () => {

describe('directoryExists', () => {
it('returns true if the given path refers to an existing directory', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const directoryPath = path.join(
sandbox.directoryPath,
Expand All @@ -416,6 +456,8 @@ describe('fs', () => {
});

it('returns false if the given path refers to something that is not a directory', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const filePath = path.join(sandbox.directoryPath, 'test.file');
await fs.promises.writeFile(filePath, 'some content');
Expand All @@ -425,6 +467,8 @@ describe('fs', () => {
});

it('returns false if the given path does not refer to any existing entry', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const directoryPath = path.join(
sandbox.directoryPath,
Expand Down Expand Up @@ -456,6 +500,8 @@ describe('fs', () => {

describe('ensureDirectoryStructureExists', () => {
it('creates directories leading up to and including the given path', async () => {
expect.assertions(3);

await withinSandbox(async (sandbox) => {
const directoryPath = path.join(sandbox.directoryPath, 'a', 'b', 'c');

Expand All @@ -477,6 +523,8 @@ describe('fs', () => {
});

it('does not throw an error, returning undefined, if the given directory already exists', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const directoryPath = path.join(sandbox.directoryPath, 'a', 'b', 'c');
await fs.promises.mkdir(path.join(sandbox.directoryPath, 'a'));
Expand All @@ -492,6 +540,8 @@ describe('fs', () => {
});

it('re-throws a wrapped version of any error that occurs, assigning it the same code and giving it a stack', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
// Make sandbox root directory non-readable
await fs.promises.chmod(sandbox.directoryPath, 0o600);
Expand Down Expand Up @@ -519,6 +569,8 @@ describe('fs', () => {
describe('forceRemove', () => {
describe('given a file path', () => {
it('removes the file', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const filePath = path.join(sandbox.directoryPath, 'test.file');
await fs.promises.writeFile(filePath, 'some content');
Expand All @@ -528,8 +580,11 @@ describe('fs', () => {
});

it('does nothing if the path does not exist', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const filePath = path.join(sandbox.directoryPath, 'test.file');

expect(await forceRemove(filePath)).toBeUndefined();
});
});
Expand Down Expand Up @@ -559,6 +614,8 @@ describe('fs', () => {

describe('given a directory path', () => {
it('removes the directory', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const directoryPath = path.join(
sandbox.directoryPath,
Expand All @@ -571,11 +628,14 @@ describe('fs', () => {
});

it('does nothing if the path does not exist', async () => {
expect.assertions(1);

await withinSandbox(async (sandbox) => {
const directoryPath = path.join(
sandbox.directoryPath,
'test-directory',
);

expect(await forceRemove(directoryPath)).toBeUndefined();
});
});
Expand Down Expand Up @@ -625,6 +685,8 @@ describe('fs', () => {

describe('withinSandbox', () => {
it('creates the sandbox directory and keeps it around before its given function ends', async () => {
expect.assertions(1);

const nowTimestamp = new Date('2023-01-01').getTime();
jest.setSystemTime(nowTimestamp);
const { withinSandbox: withinTestSandbox } = createSandbox('utils-fs');
Expand Down

0 comments on commit 0f0417f

Please sign in to comment.