Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Gru Tester] Add unit test for src/babel/plugin-react-refresh.ts #7

Open
wants to merge 1 commit into
base: gru-unit-test
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/babel/plugin-react-refresh.gru.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { transformSync } from '@babel/core'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import reactRefreshPlugin from './plugin-react-refresh.ts'

vi.mock('./utils.ts', async (importOriginal) => {
const actual = await importOriginal<typeof import('./utils.ts')>()
return {
...actual,
isAtom: vi.fn((t, callee) => callee.name === 'atom'),
}
})

describe('reactRefreshPlugin', () => {
const babel = { types: {} } as any

it('should throw an error if filename is not provided', () => {
expect(() => {
transformSync('const x = 1;', {
plugins: [[reactRefreshPlugin, { types: babel.types }]],
})
}).toThrow(
'Configuration contains string/RegExp pattern, but no filename was passed to Babel',
)
})

it('should add jotaiAtomCache to the program body', () => {
const result = transformSync('const x = 1;', {
filename: 'test.js',
plugins: [[reactRefreshPlugin, { types: babel.types }]],
})
expect(result?.code).toContain('globalThis.jotaiAtomCache')
})

it('should transform default export atom', () => {
const code = 'export default atom();'
const result = transformSync(code, {
filename: 'test.js',
plugins: [[reactRefreshPlugin, { types: babel.types }]],
})
expect(result?.code).toContain(
'exports.default = globalThis.jotaiAtomCache.get',
)
})

it('should transform variable declarator atom', () => {
const code = 'const myAtom = atom();'
const result = transformSync(code, {
filename: 'test.js',
plugins: [[reactRefreshPlugin, { types: babel.types }]],
})
expect(result?.code).toContain(
'const myAtom = globalThis.jotaiAtomCache.get',
)
})
})