From 8132de6660defa34936a698a24c72ff94c83874c Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Fri, 17 Mar 2023 14:48:21 +0100 Subject: [PATCH] refactor `log.warn` checks in tests --- tests/blocklist.test.js | 12 +------- tests/evaluateTailwindFunctions.test.js | 21 ++----------- tests/min-max-screen-variants.test.js | 40 +++++++------------------ tests/normalize-config.test.js | 8 +---- tests/warnings.test.js | 22 +++----------- 5 files changed, 18 insertions(+), 85 deletions(-) diff --git a/tests/blocklist.test.js b/tests/blocklist.test.js index f5627d20c..3642aa376 100644 --- a/tests/blocklist.test.js +++ b/tests/blocklist.test.js @@ -1,15 +1,6 @@ -import log from '../src/util/log' import { crosscheck, run, html, css } from './util/run' crosscheck(({ stable, oxide }) => { - let warn - - beforeEach(() => { - warn = jest.spyOn(log, 'warn') - }) - - afterEach(() => warn.mockClear()) - it('can block classes matched literally', () => { let config = { content: [ @@ -102,8 +93,7 @@ crosscheck(({ stable, oxide }) => { } `) - expect(warn).toHaveBeenCalledTimes(1) - expect(warn.mock.calls.map((x) => x[0])).toEqual(['blocklist-invalid']) + expect().toHaveBeenWarnedWith(['blocklist-invalid']) }) it('can block classes generated by the safelist', () => { diff --git a/tests/evaluateTailwindFunctions.test.js b/tests/evaluateTailwindFunctions.test.js index 5b41a7030..e4085e03e 100644 --- a/tests/evaluateTailwindFunctions.test.js +++ b/tests/evaluateTailwindFunctions.test.js @@ -2,7 +2,6 @@ import fs from 'fs' import path from 'path' import postcss from 'postcss' import plugin from '../src/lib/evaluateTailwindFunctions' -import log from '../src/util/log' import { crosscheck, run as runFull, html, css } from './util/run' function run(input, opts = {}) { @@ -1348,14 +1347,6 @@ crosscheck(({ stable, oxide }) => { ) afterEach(() => fs.promises.unlink(configPath)) - let warn - - beforeEach(() => { - warn = jest.spyOn(log, 'warn') - }) - - afterEach(() => warn.mockClear()) - oxide.test.todo('should not generate when theme fn doesnt resolve') stable.test('should not generate when theme fn doesnt resolve', async () => { await fs.promises.writeFile( @@ -1378,11 +1369,7 @@ crosscheck(({ stable, oxide }) => { `) // 2. But we get a warning in the console - expect(warn).toHaveBeenCalledTimes(2) - expect(warn.mock.calls.map((x) => x[0])).toEqual([ - 'invalid-theme-key-in-class', - 'invalid-theme-key-in-class', - ]) + expect().toHaveBeenWarnedWith(['invalid-theme-key-in-class']) // 3. The second run should work fine because it's been removed from the class cache result = await runFull('@tailwind utilities', configPath) @@ -1394,11 +1381,7 @@ crosscheck(({ stable, oxide }) => { `) // 4. But we've not received any further logs about it - expect(warn).toHaveBeenCalledTimes(2) - expect(warn.mock.calls.map((x) => x[0])).toEqual([ - 'invalid-theme-key-in-class', - 'invalid-theme-key-in-class', - ]) + expect().toHaveBeenWarnedWith(['invalid-theme-key-in-class']) }) }) }) diff --git a/tests/min-max-screen-variants.test.js b/tests/min-max-screen-variants.test.js index f55789aa7..a86c6f6cd 100644 --- a/tests/min-max-screen-variants.test.js +++ b/tests/min-max-screen-variants.test.js @@ -1,17 +1,6 @@ -import log from '../src/util/log' import { crosscheck, run, html, css } from './util/run' crosscheck(() => { - let warn - - beforeEach(() => { - warn = jest.spyOn(log, 'warn') - }) - - afterEach(() => { - warn.mockClear() - }) - let defaultScreens = { sm: '640px', md: '768px', @@ -119,7 +108,7 @@ crosscheck(() => { } `) - expect(warn).toHaveBeenCalledTimes(0) + expect().not.toHaveBeenWarned() }) it('works when using max variants screens config is empty and variants all use the same unit', () => { @@ -318,8 +307,7 @@ crosscheck(() => { } `) - expect(warn).toHaveBeenCalledTimes(1) - expect(warn.mock.calls.map((x) => x[0])).toEqual(['complex-screen-config']) + expect().toHaveBeenWarnedWith(['complex-screen-config']) }) it('warns when using min variants with simple configs containing mixed units', async () => { @@ -365,8 +353,7 @@ crosscheck(() => { } `) - expect(warn).toHaveBeenCalledTimes(1) - expect(warn.mock.calls.map((x) => x[0])).toEqual(['mixed-screen-units']) + expect().toHaveBeenWarnedWith(['mixed-screen-units']) }) it('warns when using min variants with mixed units (with screens config)', async () => { @@ -412,8 +399,7 @@ crosscheck(() => { } `) - expect(warn).toHaveBeenCalledTimes(1) - expect(warn.mock.calls.map((x) => x[0])).toEqual(['minmax-have-mixed-units']) + expect().toHaveBeenWarnedWith(['minmax-have-mixed-units']) }) it('warns when using min variants with mixed units (with no screens config)', async () => { @@ -450,8 +436,7 @@ crosscheck(() => { } `) - expect(warn).toHaveBeenCalledTimes(1) - expect(warn.mock.calls.map((x) => x[0])).toEqual(['minmax-have-mixed-units']) + expect().toHaveBeenWarnedWith(['minmax-have-mixed-units']) }) it('warns when using max variants with complex screen configs', async () => { @@ -500,8 +485,7 @@ crosscheck(() => { } `) - expect(warn).toHaveBeenCalledTimes(1) - expect(warn.mock.calls.map((x) => x[0])).toEqual(['complex-screen-config']) + expect().toHaveBeenWarnedWith(['complex-screen-config']) }) it('warns when using max variants with simple configs containing mixed units', async () => { @@ -547,8 +531,7 @@ crosscheck(() => { } `) - expect(warn).toHaveBeenCalledTimes(1) - expect(warn.mock.calls.map((x) => x[0])).toEqual(['mixed-screen-units']) + expect().toHaveBeenWarnedWith(['mixed-screen-units']) }) it('warns when using max variants with mixed units (with screens config)', async () => { @@ -594,8 +577,7 @@ crosscheck(() => { } `) - expect(warn).toHaveBeenCalledTimes(1) - expect(warn.mock.calls.map((x) => x[0])).toEqual(['minmax-have-mixed-units']) + expect().toHaveBeenWarnedWith(['minmax-have-mixed-units']) }) it('warns when using max variants with mixed units (with no screens config)', async () => { @@ -632,8 +614,7 @@ crosscheck(() => { } `) - expect(warn).toHaveBeenCalledTimes(1) - expect(warn.mock.calls.map((x) => x[0])).toEqual(['minmax-have-mixed-units']) + expect().toHaveBeenWarnedWith(['minmax-have-mixed-units']) }) it('warns when using min and max variants with mixed units (with no screens config)', async () => { @@ -670,7 +651,6 @@ crosscheck(() => { } `) - expect(warn).toHaveBeenCalledTimes(1) - expect(warn.mock.calls.map((x) => x[0])).toEqual(['minmax-have-mixed-units']) + expect().toHaveBeenWarnedWith(['minmax-have-mixed-units']) }) }) diff --git a/tests/normalize-config.test.js b/tests/normalize-config.test.js index dff357a81..a0b7a59bb 100644 --- a/tests/normalize-config.test.js +++ b/tests/normalize-config.test.js @@ -1,6 +1,5 @@ import { normalizeConfig } from '../src/util/normalizeConfig' import resolveConfig from '../src/public/resolve-config' -import log from '../src/util/log' import { crosscheck, run, css } from './util/run' crosscheck(({ stable, oxide }) => { @@ -128,8 +127,6 @@ crosscheck(({ stable, oxide }) => { }) it('should warn when we detect invalid globs with incorrect brace expansion', () => { - let spy = jest.spyOn(log, 'warn') - let config = { content: [ './{example-folder}/**/*.{html,js}', @@ -162,9 +159,6 @@ crosscheck(({ stable, oxide }) => { transform: {}, }) - // But a warning should happen - expect(spy).toHaveBeenCalledTimes(2) - expect(spy.mock.calls.map((x) => x[0])).toEqual(['invalid-glob-braces', 'invalid-glob-braces']) - spy.mockRestore() + expect().toHaveBeenWarnedWith(['invalid-glob-braces']) }) }) diff --git a/tests/warnings.test.js b/tests/warnings.test.js index d288a282a..e3ea94fe5 100644 --- a/tests/warnings.test.js +++ b/tests/warnings.test.js @@ -1,17 +1,6 @@ -import log from '../src/util/log' import { crosscheck, run, html, css } from './util/run' crosscheck(() => { - let warn - - beforeEach(() => { - warn = jest.spyOn(log, 'warn') - }) - - afterEach(() => { - warn.mockClear() - }) - test('it warns when there is no content key', async () => { let config = { corePlugins: { preflight: false }, @@ -23,8 +12,7 @@ crosscheck(() => { await run(input, config) - expect(warn).toHaveBeenCalledTimes(1) - expect(warn.mock.calls.map((x) => x[0])).toEqual(['content-problems']) + expect().toHaveBeenWarnedWith(['content-problems']) }) test('it warns when there is an empty content key', async () => { @@ -39,8 +27,7 @@ crosscheck(() => { await run(input, config) - expect(warn).toHaveBeenCalledTimes(1) - expect(warn.mock.calls.map((x) => x[0])).toEqual(['content-problems']) + expect().toHaveBeenWarnedWith(['content-problems']) }) test('it warns when there are no utilities generated', async () => { @@ -55,8 +42,7 @@ crosscheck(() => { await run(input, config) - expect(warn).toHaveBeenCalledTimes(1) - expect(warn.mock.calls.map((x) => x[0])).toEqual(['content-problems']) + expect().toHaveBeenWarnedWith(['content-problems']) }) it('warnings are not thrown when only variant utilities are generated', async () => { @@ -71,6 +57,6 @@ crosscheck(() => { await run(input, config) - expect(warn).toHaveBeenCalledTimes(0) + expect().not.toHaveBeenWarned() }) })