Skip to content

Commit

Permalink
Merge pull request #253 from mizdra/fix-failing-test
Browse files Browse the repository at this point in the history
Fix failing tests
  • Loading branch information
mizdra authored May 25, 2024
2 parents 258cd4e + d8529fd commit 6133916
Showing 1 changed file with 15 additions and 29 deletions.
44 changes: 15 additions & 29 deletions packages/happy-css-modules/src/locator/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
import fs, { readFile, writeFile } from 'fs/promises';
import { readFile, writeFile } from 'fs/promises';
import { randomUUID } from 'node:crypto';
import { jest } from '@jest/globals';
import dedent from 'dedent';
import { createDefaultTransformer } from '../index.js';
import { Locator, createDefaultTransformer } from '../index.js';
import { createFixtures, getFixturePath } from '../test-util/util.js';
import { sleepSync } from '../util.js';

const readFileSpy = jest.spyOn(fs, 'readFile');
// In ESM, for some reason, we need to explicitly mock module
jest.unstable_mockModule('fs/promises', () => ({
...fs, // Inherit native functions (e.g., fs.stat)
readFile: readFileSpy,
}));

// After the mock of fs/promises is complete, . /index.js after the mock of fs/promises is complete.
// ref: https://www.coolcomputerclub.com/posts/jest-hoist-await/
const { Locator } = await import('./index.js');
// NOTE: ../test/util.js depends on . /index.js, so it must also be imported dynamically...

const locator = new Locator();

afterEach(() => {
readFileSpy.mockClear();
});

test('basic', async () => {
createFixtures({
'/test/1.css': dedent`
Expand Down Expand Up @@ -186,7 +170,7 @@ test('normalizes tokens', async () => {
`);
});

test.failing('returns the result from the cache when the file has not been modified', async () => {
test('returns the result from the cache when the file has not been modified', async () => {
createFixtures({
'/test/1.css': dedent`
@import './2.css';
Expand All @@ -200,30 +184,32 @@ test.failing('returns the result from the cache when the file has not been modif
.d {}
`,
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const readCSSSpy = jest.spyOn(locator, 'readCSS' as any);
await locator.load(getFixturePath('/test/1.css'));
expect(readFileSpy).toHaveBeenCalledTimes(3);
expect(readFileSpy).toHaveBeenNthCalledWith(1, '/test/1.css', 'utf-8');
expect(readFileSpy).toHaveBeenNthCalledWith(2, '/test/2.css', 'utf-8');
expect(readFileSpy).toHaveBeenNthCalledWith(3, '/test/3.css', 'utf-8');
readFileSpy.mockClear();
expect(readCSSSpy).toHaveBeenCalledTimes(3);
expect(readCSSSpy).toHaveBeenNthCalledWith(1, getFixturePath('/test/1.css'));
expect(readCSSSpy).toHaveBeenNthCalledWith(2, getFixturePath('/test/2.css'));
expect(readCSSSpy).toHaveBeenNthCalledWith(3, getFixturePath('/test/3.css'));
readCSSSpy.mockClear();

// update `/test/2.css`
sleepSync(1); // wait for the file system to update the mtime
await writeFile(getFixturePath('/test/2.css'), await readFile(getFixturePath('/test/2.css'), 'utf-8'));

// `3.css` is not updated, so the cache is used. Therefore, `readFile` is not called.
await locator.load(getFixturePath('/test/3.css'));
expect(readFileSpy).toHaveBeenCalledTimes(0);
expect(readCSSSpy).toHaveBeenCalledTimes(0);

// `1.css` is not updated, but dependencies are updated, so the cache is used. Therefore, `readFile` is called.
await locator.load(getFixturePath('/test/1.css'));
expect(readFileSpy).toHaveBeenCalledTimes(2);
expect(readFileSpy).toHaveBeenNthCalledWith(1, '/test/1.css', 'utf-8');
expect(readFileSpy).toHaveBeenNthCalledWith(2, '/test/2.css', 'utf-8');
expect(readCSSSpy).toHaveBeenCalledTimes(2);
expect(readCSSSpy).toHaveBeenNthCalledWith(1, getFixturePath('/test/1.css'));
expect(readCSSSpy).toHaveBeenNthCalledWith(2, getFixturePath('/test/2.css'));

// ``2.css` is updated, but the cache is already available because it was updated in the previous step. Therefore, `readFile` is not called.
await locator.load(getFixturePath('/test/2.css'));
expect(readFileSpy).toHaveBeenCalledTimes(2);
expect(readCSSSpy).toHaveBeenCalledTimes(2);
});

describe('supports sourcemap', () => {
Expand Down

0 comments on commit 6133916

Please sign in to comment.