Skip to content
This repository has been archived by the owner on Feb 25, 2022. It is now read-only.

Commit

Permalink
feat:lots of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zoy-l committed Mar 4, 2021
1 parent 9ed6c00 commit b312286
Show file tree
Hide file tree
Showing 70 changed files with 4,355 additions and 3,580 deletions.
2 changes: 1 addition & 1 deletion .yarnrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Using the mirror source

# registry "https://registry.npm.taobao.org"
registry "https://registry.npm.taobao.org"
4 changes: 2 additions & 2 deletions examples/normal-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
"license": "MIT",
"dependencies": {
"@vuedx/typescript-plugin-vue": "^0.6.0",
"vue": "^3.0.5"
"vue": "^3.0.7"
},
"scripts": {
"dev": "zmi dev",
"build": "zmi build"
},
"devDependencies": {
"@vue/compiler-sfc": "^3.0.5"
"@vue/compiler-sfc": "^3.0.7"
}
}
920 changes: 461 additions & 459 deletions examples/normal-vue/yarn.lock

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@ import { jestConfig } from 'zmi-nerd'

export default {
collectCoverageFrom(memo) {
return memo.concat(['!packages/zmi-preset/src/plugins/features/*'])
return memo.concat([
'!packages/zmi-preset/src/plugins/features/*',
'!packages/zmi-preset/src/plugins/commands/*',
'!packages/zmi-preset/src/plugins/common/BundleUtils.ts',
'!packages/zmi-preset/src/plugins/common/generateHtml.ts',
'!packages/zmi-preset/src/plugins/registerMethods.ts',

'!packages/zmi/src/cli.ts',
'!packages/zmi/src/forkedDev.ts',
'!packages/zmi/src/fork.ts',
'!packages/zmi-create-app/src/cli.ts',

'!packages/zmi-utils/src/index.ts',
'!packages/zmi-utils/src/launchDevice.ts',
'!packages/zmi-utils/src/clearConsole.ts'
])
}
} as jestConfig
8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@
},
"devDependencies": {
"@babel/core": "^7.13.8",
"@octokit/core": "^3.2.5",
"@vue/compiler-sfc": "3.0.5",
"lerna": "^4.0.0",
"rimraf": "^3.0.2",
"typescript": "^4.2.2",
"zmi-nerd": "^1.1.8"
},
"dependencies": {
"@types/gulp-sourcemaps": "^0.0.34"
"zmi-nerd": "^1.1.8",
"@vue/compiler-sfc": "^3.0.7"
}
}
289 changes: 289 additions & 0 deletions packages/zmi-babel-factory/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
import { deepmerge, slash } from '@zmi-cli/utils'
import { join } from 'path'
import { transform } from '@babel/core'
import { Ioptions } from './index'

const DEFAULT_OPTS = {
env: {
modules: 'commonjs'
}
}

export function transformWithPreset(code: string, opts: Ioptions) {
const filename = opts.typescript ? 'file.ts' : 'file.js'
return transform(code, {
filename,
presets: [[require.resolve('./index.ts'), deepmerge(DEFAULT_OPTS, opts)]]
})!.code
}

test('cjs', () => {
const code = transformWithPreset(`import { a } from './a';`, {})
expect(code).toContain('var _a = require("./a");')
})

test('esm', () => {
const code = transformWithPreset(`import { a } from './a';`, {
env: {
modules: false
}
})
expect(code).toContain(`import { a } from './a';`)
})

test('typescript', () => {
const code = transformWithPreset(
`
const a: string = 'foo'; console.log(a);
`,
{
typescript: true
}
)
expect(code).toContain(`var a = 'foo';`)
})

test('typescript with namespace', () => {
const code = transformWithPreset(
`
namespace N {
export const V = 1;
}
`,
{
typescript: true
}
)
expect(code).toContain(`var V = _N.V = 1;`)
})

test('typescript with metadata', () => {
const code = transformWithPreset(
`@Decorate
class MyClass {
constructor(
private generic: Generic<A>,
generic2: Generic<A, B>
) {}
@Run
method(
generic: Inter<A>,
@Arg() generic2: InterGen<A, B>
) {}
}`,
{
typescript: true
}
)
expect(code).toContain('Reflect.metadata')
})

test('typescript with nest-injection', () => {
const code = transformWithPreset(
`import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private appService: AppService) {}
@Inject()
appService: AppService;
@Inject()
private appService2: AppService;
@Get()
getHello(): string {
return this.appService.getHello();
}
}`,
{
typescript: true
}
)
expect(code).toContain('Reflect.metadata')
expect(code).toContain('_initializerDefineProperty(this, "appService", _descriptor, this);')
expect(code).toContain('_initializerDefineProperty(this, "appService2", _descriptor2, this);')
})

test('typescript key remapping types', () => {
const code = transformWithPreset(
`type Options = {
[K in "noImplicitAny" | "strictNullChecks" | "strictFunctionTypes"]?: boolean
};`,
{
typescript: true
}
)
expect(code).toContain('"use strict"')
})

test('dynamic import', () => {
const code = transformWithPreset(`import('./a');`, {})
expect(code).toContain(`require('./a')`)
})

test('object spread', () => {
const code = transformWithPreset(`const a = { ...b };`, {
env: {
targets: { ie: 10 }
}
})
expect(code).toContain(`_objectSpread({}, b);`)
})

test('optional catch binding', () => {
const code = transformWithPreset(`try { throw e } catch {}`, {
env: {
targets: { ie: 10 }
}
})
expect(code).toContain(`catch (_unused) {}`)
})

test('async generator function', () => {
const code = transformWithPreset(`async function* agf() { await 111; yield 222; }`, {
env: {
targets: { ie: 10 }
}
})
expect(code).toContain(`return _awaitAsyncGenerator(111);`)
})

test('decorators', () => {
const code = transformWithPreset(`@foo class Foo {}`, {
env: {
targets: { ie: 10 }
}
})
expect(code).toContain(`foo(_class = function Foo() {`)
})

test('class properties', () => {
const code = transformWithPreset(`class Foo { a = 'b'; foo = () => this.a; static c = 'd';}`, {
env: {
targets: { ie: 10 }
}
})
expect(code).toContain(`this.a = 'b';`)
})

test('export default from', () => {
const code = transformWithPreset(`export v from 'a'`, {
env: {
targets: { ie: 10 }
}
})
expect(code).toContain(`Object.defineProperty(exports, "v", {`)
})

test('nullish coalescing operator', () => {
const code = transformWithPreset(`const a = foo.bar ?? 'hoo';`, {
env: {
targets: { ie: 10 }
}
})
expect(code).toContain(`var a = (_foo$bar = foo.bar) !== null &&`)
})

test('optional chaining', () => {
const code = transformWithPreset(`const a = b?.c?.d;`, {
env: {
targets: { ie: 10 }
}
})
expect(code).toContain(`var a = (_b = b) === null || _b`)
})

test('pipeline operator', () => {
const code = transformWithPreset(`const a = b |> c |> d;`, {
env: {
targets: { ie: 10 }
}
})
expect(code).toContain(`var a = (_ref = (_b = b, c(_b)), d(_ref));`)
})

test('do expression', () => {
const code = transformWithPreset(`const a = do { if (foo) 'foo'; else 'bar'; }`, {
env: {
targets: { ie: 10 }
}
})
expect(code).toContain(`var a = foo ? 'foo' : 'bar';`)
})

test('function bind', () => {
const code = transformWithPreset(`a::b; ::a.b; a::b(c); ::a.b(c);`, {
env: {
targets: { ie: 10 }
}
})
expect(code).toContain(`(_context = a, b).bind(_context);`)
})

test('transform runtime', () => {
const code = transformWithPreset(`class A {}`, {
env: {
targets: { ie: 10 }
},
transformRuntime: {}
})
expect(slash(join(code!))).toContain(`node_modules/@babel/runtime/helpers/esm/classCallCheck"));`)
})

test('babel-plugin-auto-css-modules', () => {
const code = transformWithPreset(`import styles from './a.css';`, {
env: {
targets: { ie: 10 }
},
autoCSSModules: true
}) as string

expect(/\.\/a.css\?module/.test(code)).toEqual(true)
})

test('logical assignment operators', () => {
const code = transformWithPreset(`a ||= b;`, {
env: {
targets: { ie: 10 }
}
})
expect(slash(code!)).toContain(`a || (a = b);`)
})

test('top level await', () => {
const code = transformWithPreset(`await delay(1000);`, {
env: {
targets: { ie: 10 }
}
})
expect(code).toContain(`await delay(1000);`)
})

test('babel-plugin-dynamic-import-node', () => {
const code = transformWithPreset(`import('./foo.js').then();`, {
env: {
targets: { ie: 10 }
},
dynamicImportNode: true
})

expect(code).toContain(`_interopRequireWildcard(require('./foo.js'));`)
})

test('modify', () => {
const code = transformWithPreset(`import('./foo.js').then();`, {
modify: (preset) => {
// @ts-expect-error test
preset.plugins.push(require.resolve('babel-plugin-dynamic-import-node'))
return preset
},
env: {
targets: { ie: 10 }
}
})

expect(code).toContain(`_interopRequireWildcard(require('./foo.js'));`)
})
13 changes: 3 additions & 10 deletions packages/zmi-babel-factory/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ export interface Ioptions {
modify?: <T>(value: T) => T
}

export function isObject<T extends Record<string, any>>(
obj: T | boolean
): T | Partial<T> {
export function isObject<T extends Record<string, any>>(obj: T | boolean): T | Partial<T> {
return typeof obj === 'object' ? obj : {}
}

Expand Down Expand Up @@ -50,15 +48,10 @@ export default (_context: never, options: Ioptions) => {
].filter(Boolean),
plugins: [
[require.resolve('@babel/plugin-proposal-optional-chaining'), { loose: false }],
[
require.resolve('@babel/plugin-proposal-nullish-coalescing-operator'),
{ loose: false }
],
[require.resolve('@babel/plugin-proposal-nullish-coalescing-operator'), { loose: false }],
require.resolve('@babel/plugin-syntax-top-level-await'),
[require.resolve('@babel/plugin-transform-destructuring'), { loose: false }],
options.typescript && [
require.resolve('babel-plugin-transform-typescript-metadata')
],
options.typescript && [require.resolve('babel-plugin-transform-typescript-metadata')],
[require.resolve('@babel/plugin-proposal-decorators'), { legacy: true }],
[require.resolve('@babel/plugin-proposal-class-properties'), { loose: true }],
require.resolve('@babel/plugin-proposal-export-default-from'),
Expand Down
Loading

0 comments on commit b312286

Please sign in to comment.