How to write type-tests for components? #209
-
Coming from this example. I would like to write a test that the two images in the bottom work in exactly that way - i.e. passing an array of numbers yields a variable which has |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
This is a good question. One option would be to write something like: const wantsANumber = helper((positional: [value: number]) => {});
interface MyTestContext {
wantsANumber: typeof wantsANumber;
}
test('the yielded type depends on the input array', async function (this: MyTestContext, assert) {
this.wantsANumber = wantsANumber;
await render<MyTestContext>(hbs`
<Foo @foos={{array 1 2 3}} as |foo|>
{{this.wantsANumber foo}}
</Foo>
<Foo @foos={{array "a" "b" "c"}} as |foo|>
{{! @glint-expect-error: no strings allowed! }}
{{this.wantsANumber foo}}
</Foo>
`);
assert.ok(true);
}); Of course you don't have to write it like an actual runtime test—you could put Another option is to do what it looks like they've done in some of the tests in glint-template-types and use Glint's own internals along with something like It might be interesting to explore something like import { expectTypeOf } from 'expect-type';
import { hbs } from '@glimmerx/component';
const foos = [1, 2, 3];
hbs`
<Foo @foos={{foos}} as |foo|>
{{(expectTypeOf foo).toEqualTypeOf 0}}
{{(expectTypeOf foo).not.toEqualTypeOf "hello"}}
</Foo>
`; |
Beta Was this translation helpful? Give feedback.
-
The "right" way to write type-tests is now with the built-in type-test package. |
Beta Was this translation helpful? Give feedback.
The "right" way to write type-tests is now with the built-in type-test package.