rsnext/test/unit/next-swc.test.ts
JJ Kasper 005b13f1ac
Move unit tests to one folder and migrate them to TypeScript (#28427)
* Move unit tests to one folder

* Migrate unit tests to TypeScript

* add test types to lint

* Ensure ts(x) tests are run with util

* Add tsx extension to jest config

* bump
2021-08-24 07:52:45 -05:00

51 lines
1.3 KiB
TypeScript

/* eslint-env jest */
import { transform } from 'next/dist/build/swc'
const swc = async (code) => {
let output = await transform(code)
return output.code
}
const trim = (s) => s.join('\n').trim().replace(/^\s+/gm, '')
describe('next/swc', () => {
describe('hook_optimizer', () => {
it('should transform Array-destructured hook return values use object destructuring', async () => {
const output = await swc(
trim`
import { useState } from 'react';
const [count, setCount] = useState(0);
`
)
expect(output).toMatch(trim`
var ref = useState(0), count = ref[0], setCount = ref[1];
`)
expect(output).toMatchInlineSnapshot(`
"import { useState } from 'react';
var ref = useState(0), count = ref[0], setCount = ref[1];
"
`)
})
it('should be able to ignore some Array-destructured hook return values', async () => {
const output = await swc(
trim`
import { useState } from 'react';
const [, setCount] = useState(0);
`
)
expect(output).toMatch(trim`
var ref = useState(0), setCount = ref[1];
`)
expect(output).toMatchInlineSnapshot(`
"import { useState } from 'react';
var ref = useState(0), setCount = ref[1];
"
`)
})
})
})