Skip to content

Commit

Permalink
test: update test file to iterate rather than use .each
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jun 6, 2023
1 parent 7264173 commit 9df1adc
Showing 1 changed file with 53 additions and 54 deletions.
107 changes: 53 additions & 54 deletions test/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,49 @@ import { parse } from 'acorn'

import { MagicRegExpTransformPlugin } from '../src/transform'

describe.each([
{ importSpecifier: 'magic-regexp' },
{ importSpecifier: 'magic-regexp/further-magic' },
])('transformer', ({ importSpecifier }) => {
const couldTransform = [
`import { createRegExp, exactly, anyOf } from '${importSpecifier}'`,
"const re1 = createRegExp(exactly('bar').notBefore('foo'))",
]
for (const importSpecifier of ['magic-regexp', 'magic-regexp/further-magic']) {
describe(`transformer: ${importSpecifier}`, () => {
const couldTransform = [
`import { createRegExp, exactly, anyOf } from '${importSpecifier}'`,
"const re1 = createRegExp(exactly('bar').notBefore('foo'))",
]

it('ignores non-JS files', () => {
expect(transform(couldTransform, 'test.css')).toBeUndefined()
})
it('ignores non-JS files', () => {
expect(transform(couldTransform, 'test.css')).toBeUndefined()
})

it('transforms vue script blocks', () => {
expect(transform(couldTransform, 'test.vue?type=script')).toBeDefined()
expect(transform(couldTransform, 'test.vue')).toBeDefined()
expect(transform(couldTransform, 'test.vue?type=template')).toBeUndefined()
})
it('transforms vue script blocks', () => {
expect(transform(couldTransform, 'test.vue?type=script')).toBeDefined()
expect(transform(couldTransform, 'test.vue')).toBeDefined()
expect(transform(couldTransform, 'test.vue?type=template')).toBeUndefined()
})

it(`ignores code without imports from ${importSpecifier}`, () => {
expect(transform(couldTransform[1])).toBeUndefined()
expect(transform([`// ${importSpecifier}`, couldTransform[1]])).toBeUndefined()
})
it(`ignores code without imports from ${importSpecifier}`, () => {
expect(transform(couldTransform[1])).toBeUndefined()
expect(transform([`// ${importSpecifier}`, couldTransform[1]])).toBeUndefined()
})

it('preserves context for dynamic regexps', () => {
expect(
transform([
`import { createRegExp } from '${importSpecifier}'`,
`console.log(createRegExp(anyOf(keys)))`,
])
).not.toBeDefined()
})
it('preserves context for dynamic regexps', () => {
expect(
transform([
`import { createRegExp } from '${importSpecifier}'`,
`console.log(createRegExp(anyOf(keys)))`,
])
).not.toBeDefined()
})

it('statically replaces regexps where possible', () => {
const code = transform([
"import { something } from 'other-module'",
`import { createRegExp, exactly, anyOf } from '${importSpecifier}'`,
'//', // this lets us tree-shake the import for use in our test-suite
"const re1 = createRegExp(exactly('bar').notBefore('foo'))",
"const re2 = createRegExp(anyOf(exactly('bar'), 'foo'))",
"const re3 = createRegExp('/foo/bar')",
// This line will be double-escaped in the snapshot
"re3.test('/foo/bar')",
])
expect(code).toMatchInlineSnapshot(`
it('statically replaces regexps where possible', () => {
const code = transform([
"import { something } from 'other-module'",
`import { createRegExp, exactly, anyOf } from '${importSpecifier}'`,
'//', // this lets us tree-shake the import for use in our test-suite
"const re1 = createRegExp(exactly('bar').notBefore('foo'))",
"const re2 = createRegExp(anyOf(exactly('bar'), 'foo'))",
"const re3 = createRegExp('/foo/bar')",
// This line will be double-escaped in the snapshot
"re3.test('/foo/bar')",
])
expect(code).toMatchInlineSnapshot(`
"import { something } from 'other-module'
import { createRegExp, exactly, anyOf } from '${importSpecifier}'
//
Expand All @@ -56,29 +54,30 @@ describe.each([
const re3 = /\\\\/foo\\\\/bar/
re3.test('/foo/bar')"
`)
// ... but we test it here.
expect(eval(code.split('//').pop())).toMatchInlineSnapshot('true')
})
// ... but we test it here.
expect(eval(code.split('//').pop())).toMatchInlineSnapshot('true')
})

it('respects how users import library', () => {
const code = transform([
`import { createRegExp as cRE } from '${importSpecifier}'`,
`import { exactly as ext, createRegExp } from '${importSpecifier}'`,
`import * as magicRE from '${importSpecifier}'`,
"const re1 = cRE(ext('bar').notBefore('foo'))",
"const re2 = magicRE.createRegExp(magicRE.anyOf('bar', 'foo'))",
"const re3 = createRegExp('test/value')",
])
expect(code).toMatchInlineSnapshot(`
it('respects how users import library', () => {
const code = transform([
`import { createRegExp as cRE } from '${importSpecifier}'`,
`import { exactly as ext, createRegExp } from '${importSpecifier}'`,
`import * as magicRE from '${importSpecifier}'`,
"const re1 = cRE(ext('bar').notBefore('foo'))",
"const re2 = magicRE.createRegExp(magicRE.anyOf('bar', 'foo'))",
"const re3 = createRegExp('test/value')",
])
expect(code).toMatchInlineSnapshot(`
"import { createRegExp as cRE } from '${importSpecifier}'
import { exactly as ext, createRegExp } from '${importSpecifier}'
import * as magicRE from '${importSpecifier}'
const re1 = /bar(?!foo)/
const re2 = /(?:bar|foo)/
const re3 = /test\\\\/value/"
`)
})
})
})
}

const transform = (code: string | string[], id = 'some-id.js') => {
const plugin = MagicRegExpTransformPlugin.vite() as any
Expand Down

1 comment on commit 9df1adc

@vercel
Copy link

@vercel vercel bot commented on 9df1adc Jun 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.