1.0.0-alpha • Published 2 years ago

@typed-macro/test-utils v1.0.0-alpha

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@typed-macro/test-utils

For basic concepts, see documentation for typed-macro.

Utils

TestTransformer

TestTransformer is a wrapper of the Transformer inside the Runtime.

type TestTransformer = {
  (ctx: TestTransformerContext): string
  (code: string): string
}

Here is an example about using TestTransformer with Jest:

import {
  createTestEnvContext,
  createTestTransformer,
} from '@typed-macro/test-utils'

const devEnv = createTestEnvContext({ dev: true })
const transform = createTestTransformer({
  macros: {
    '@macros': [...someMacros],
  },
})
expect(transform(`...some code`)).toMatchSnapshot()
expect(transform({ code: `...some code`, env: devEnv })).toMatchSnapshot()

TestTypeRenderer

TestTypeRenderer is a wrapper of the type renderer in Runtime, returning the result as a string rather than writing to a file.

Here is an example about using TestTypeRenderer with Jest:

import { createTestTypeRenderer } from '@typed-macro/test-utils'

const yourExports = {
  '@load': {
    macros: [...someMacros],
  },
  '@helper': {
    code: `export const a = 1`,
    customTypes: `export const a: number`,
  },
}
const render = createTestTypeRenderer()
expect(render(yourExports)).toMatchSnapshot()

TestRuntime

TestRuntime is a wrapper of the Runtime with more options and more friendly methods for test.

const runtime = createTestRuntime({
  provider: [yourProvider],
  env: createTestEnvContext({ dev: false }),
})

expect(await runtime.transform(`...`, 'test.ts')).toMatchSnapshot()