A test context for @a-la packages.
npm install @a-la/context
@a-la/context is a a test context for @a-la packages used in alamode transpiler. It will provide a means to record the result of transforms for given rules, as well as emitted events.
``sh`
yarn add -E @a-la/context
- Table Of Contents
- API
- [async stream(rules: Rule|Rule[], text: string, eventKeys?: string[]): ReturnType](#async-streamrules-ruleruletext-stringeventkeys-string-returntype)ReturnType
* setConfig(config: Object)
- setFile(file: string)
- setProps(props: !Object)
-
- Copyright
The package is available by importing its default function:
`js`
import ÀLaContext from '@a-la/context'
The context is then passed to the context property of _Zoroaster_ tests suites.
async stream(
text: string,
eventKeys?: string[],
): ReturnTypeCreates a Replaceable stream according to a rule or set of rules, asynchronously ends it with passed text and returns the outcome.
In the example below, a transform rule is used to replace an export statement with a module.exports statement, and emit an exports event.
`jsexport function
/**
* A rule to replace an statement with module.exports.
*/
export const exportFunctionRule = {
re: / export function ([$_\w][$_\w\d])/gm,
replacement(_, fn) {
// async
this.emit('exports', fn)
// sync
this.exports = this.exports || []
this.exports.push(fn)
return module.exports.${fn} = function ${fn}`
},
}
Now, this rule can be tested using the @a-la/context and zoroaster testing framework.
`js
import { equal, deepEqual } from '@zoroaster/assert'
import ÀLaContext from '@a-la/context'
import { exportFunctionRule as rule } from '../../src/rule'
/* @type {Object.
const T = {
context: ÀLaContext,
async 'replaces the export function'({ stream }) {
const fn = 'test'
const data = export function ${fn}() {}
const {
result,
events,
replaceable,
} = await stream(rule, data, ['exports'])
const expected = module.exports.${fn} = function ${fn}() {}
equal(result, expected)
deepEqual(events, {
exports: [fn],
})
deepEqual(replaceable.exports, [fn])
},
}
export default T
`
`
example/test/spec
✓ replaces the export function
🦅 Executed 1 test.
`
The output of the stream function is an object described below.
__ReturnType__: Replaceable instance, string result and events map.
| Name | Type | Description |
| ---------------- | ------------------------------------ | -------------------------------------------------------- |
| __events*__ | Object<string, any[]> | Emitted events recorded against passed events keys. |
| __result*__ | string | The caught output of a _Replaceable_ stream as a string. |
| __replaceable*__ | Replaceable | The instance of a _Replaceable_ stream. |
setConfig(
): voidThis method allows to set the config property of the _Replaceable_ instance created inside of the stream method. Some transform may use config for certain functionality, e.g., replacing of the source string in the @a-la/import transform.
setFile(
): voidSets the file property on the replaceable stream. This is required by the import transform to find the _package.json_ file of the imported module to check whether it has the alamode property so that the esCheck can be skipped.
setProps(
): voidAssigns properties to the transform stream. The rules will be able to access them via their this` context.
alt="Art Deco"> | © Art Deco™ for À La Mode 2020 |
|---|