test(legacy): add a test to checks all inline snippets are valid JS (#15098)

This commit is contained in:
翠 / green 2023-11-22 17:42:25 +09:00 committed by GitHub
parent 1c605ffe9b
commit 1b9ca66b6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,15 +1,21 @@
import { expect, test } from 'vitest'
import { describe, expect, test } from 'vitest'
import type { ecmaVersion } from 'acorn'
import { parse } from 'acorn'
import { detectModernBrowserDetector } from '../snippets'
import {
detectModernBrowserCode,
detectModernBrowserDetector,
dynamicFallbackInlineCode,
safari10NoModuleFix,
systemJSInlineCode,
} from '../snippets'
const shouldFailVersions: ecmaVersion[] = []
for (let v = 2015; v <= 2019; v++) {
shouldFailVersions.push(v as ecmaVersion)
}
const shouldPassVersions: acorn.ecmaVersion[] = []
for (let v = 2020; v <= 2022; v++) {
const shouldPassVersions: ecmaVersion[] = []
for (let v = 2020; v <= 2024; v++) {
shouldPassVersions.push(v as ecmaVersion)
}
@ -34,3 +40,23 @@ for (const version of shouldPassVersions) {
}).not.toThrow()
})
}
describe('snippets are valid', () => {
const codes = {
safari10NoModuleFix,
systemJSInlineCode,
detectModernBrowserCode,
dynamicFallbackInlineCode,
}
for (const [name, value] of Object.entries(codes)) {
test(`${name} is valid JS`, () => {
expect(() => {
parse(value, {
ecmaVersion: 'latest',
sourceType: 'module',
})
}).not.toThrow()
})
}
})