Skip to content

Commit

Permalink
test(#22): add vitest snapshots for name inference
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenix-ru committed Apr 5, 2024
1 parent 304911d commit b0d8435
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 6 deletions.
Binary file modified crates/fervid_napi/.yarn/install-state.gz
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`SFC analyze <script> bindings > auto name inference > basic 1`] = `
"import { toDisplayString as _toDisplayString } from "vue";
export default {
__name: "FooBar",
render (_ctx, _cache, $props, $setup, $data, $options) {
return _toDisplayString(a);
},
setup () {
const a = 1;
return {
a
};
}
};
"
`;
exports[`SFC analyze <script> bindings > auto name inference > do not overwrite manual name (call) 1`] = `
"import { defineComponent } from 'vue';
import { toDisplayString as _toDisplayString } from "vue";
export default {
name: 'Baz',
render (_ctx, _cache, $props, $setup, $data, $options) {
return _toDisplayString(a);
},
setup () {
const a = 1;
return {
a
};
}
};
"
`;
exports[`SFC analyze <script> bindings > auto name inference > do not overwrite manual name (object) 1`] = `
"import { toDisplayString as _toDisplayString } from "vue";
export default {
name: 'Baz',
render (_ctx, _cache, $props, $setup, $data, $options) {
return _toDisplayString(a);
},
setup () {
const a = 1;
return {
a
};
}
};
"
`;
92 changes: 92 additions & 0 deletions crates/fervid_napi/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { describe, test, expect } from 'vitest'
import { parse as babelParse } from '@babel/parser'
import { Compiler, FervidCompileOptions } from '..'

describe('SFC analyze <script> bindings', () => {
// https://github.com/vuejs/core/blob/272ab9fbdcb1af0535108b9f888e80d612f9171d/packages/compiler-sfc/__tests__/compileScript.spec.ts#L1252-L1306
describe('auto name inference', () => {
test('basic', () => {
const { content } = compile(
`<script setup>const a = 1</script>
<template>{{ a }}</template>`,
{
filename: 'FooBar.vue',
id: ''
},
)
expect(content).toMatch(`export default {
__name: "FooBar"`)
assertCode(content)
})

test('do not overwrite manual name (object)', () => {
const { content } = compile(
`<script>
export default {
name: 'Baz'
}
</script>
<script setup>const a = 1</script>
<template>{{ a }}</template>`,
{
filename: 'FooBar.vue',
id: ''
},
)
expect(content).not.toMatch(`name: 'FooBar'`)
expect(content).toMatch(`name: 'Baz'`)
assertCode(content)
})

test('do not overwrite manual name (call)', () => {
const { content } = compile(
`<script>
import { defineComponent } from 'vue'
export default defineComponent({
name: 'Baz'
})
</script>
<script setup>const a = 1</script>
<template>{{ a }}</template>`,
{
filename: 'FooBar.vue',
id: ''
},
)
expect(content).not.toMatch(`name: 'FooBar'`)
expect(content).toMatch(`name: 'Baz'`)
assertCode(content)
})
})
})

// https://github.com/vuejs/core/blob/272ab9fbdcb1af0535108b9f888e80d612f9171d/packages/compiler-sfc/__tests__/utils.ts#L11-L24
function compile(src: string, options: FervidCompileOptions) {
const compiler = new Compiler()
const result = compiler.compileSync(src, options)

if (result.errors.length) {
console.warn(result.errors[0])
}

return {
content: result.code
}
}

function assertCode(code: string) {
// parse the generated code to make sure it is valid
try {
babelParse(code, {
sourceType: 'module',
plugins: [
'typescript',
['importAttributes', { deprecatedAssertSyntax: true }],
],
})
} catch (e: any) {
console.log(code)
throw e
}
expect(code).toMatchSnapshot()
}
17 changes: 11 additions & 6 deletions crates/fervid_napi/benchmark/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { join } from 'node:path'
import { cpus } from 'node:os'
import { compileScript, compileTemplate, parse } from '@vue/compiler-sfc'

import { Compiler } from '../index'
import { Compiler, FervidCompileOptions } from '../index'

// Increase libuv thread pool for a better async result.
// 4 threads is a default thread pool size.
Expand All @@ -18,6 +18,11 @@ const input = readFileSync(join(__dirname, '../../fervid/benches/fixtures/input.
encoding: 'utf-8',
})

const options: FervidCompileOptions = {
filename: 'input.vue',
id: ''
}

async function run() {
const compiler = new Compiler()

Expand All @@ -42,7 +47,7 @@ async function run() {
}),

b.add('@fervid/napi sync', () => {
compiler.compileSync(input)
compiler.compileSync(input, options)
}),

// The code below makes sure that async framework is not flawed.
Expand All @@ -53,24 +58,24 @@ async function run() {

b.add('@fervid/napi sync promise (4 threads)', () => {
return Promise.allSettled(
Array.from({ length: 4 }, (_) => new Promise<void>((resolve) => (compiler.compileSync(input), resolve()))),
Array.from({ length: 4 }, (_) => new Promise<void>((resolve) => (compiler.compileSync(input, options), resolve()))),
)
}),

b.add(`@fervid/napi sync promise (${CPUS} threads)`, () => {
return Promise.allSettled(
Array.from({ length: CPUS }, (_) => new Promise<void>((resolve) => (compiler.compileSync(input), resolve()))),
Array.from({ length: CPUS }, (_) => new Promise<void>((resolve) => (compiler.compileSync(input, options), resolve()))),
)
}),

// END

b.add('@fervid/napi async (4 threads)', () => {
return Promise.allSettled(Array.from({ length: 4 }, (_) => compiler.compileAsync(input)))
return Promise.allSettled(Array.from({ length: 4 }, (_) => compiler.compileAsync(input, options)))
}),

b.add(`@fervid/napi async CPUS (${CPUS} threads)`, () => {
return Promise.allSettled(Array.from({ length: CPUS }, (_) => compiler.compileAsync(input)))
return Promise.allSettled(Array.from({ length: CPUS }, (_) => compiler.compileAsync(input, options)))
}),

// Custom cycle function to account for the async nature
Expand Down
1 change: 1 addition & 0 deletions crates/fervid_napi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"version": "napi version"
},
"devDependencies": {
"@babel/parser": "^7.24.4",
"@napi-rs/cli": "^2.18.0",
"@swc-node/register": "^1.9.0",
"@swc/core": "^1.4.8",
Expand Down
10 changes: 10 additions & 0 deletions crates/fervid_napi/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ __metadata:
languageName: node
linkType: hard

"@babel/parser@npm:^7.24.4":
version: 7.24.4
resolution: "@babel/parser@npm:7.24.4"
bin:
parser: ./bin/babel-parser.js
checksum: 8381e1efead5069cb7ed2abc3a583f4a86289b2f376c75cecc69f59a8eb36df18274b1886cecf2f97a6a0dff5334b27330f58535be9b3e4e26102cc50e12eac8
languageName: node
linkType: hard

"@babel/types@npm:^7.8.3":
version: 7.23.4
resolution: "@babel/types@npm:7.23.4"
Expand Down Expand Up @@ -368,6 +377,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@fervid/napi@workspace:."
dependencies:
"@babel/parser": "npm:^7.24.4"
"@fervid/napi-android-arm-eabi": "npm:0.2.1"
"@fervid/napi-android-arm64": "npm:0.2.1"
"@fervid/napi-darwin-arm64": "npm:0.2.1"
Expand Down

0 comments on commit b0d8435

Please sign in to comment.