2017-02-23 43 views
0

私はavaのテストコンテキストを入力したいと思うので、avaを使用しています(私は2以上を使用することはできませんのでリンクはありません)。それはtyped as any in ava's definition fileです。後続の変数宣言は、同じ型でなければなりません。 any

私は特にしたいことはtypescriptですコンパイラはt.contextは、次のテストでタイプ{foo: number}であることを知っているということです。

import test from 'ava' 

test.beforeEach((t) => { 
    t.context = { foo: 5 } 
}) 

test('Is context typed', (t) => { 
    // uncaught typo 
    t.is(t.context.fooo, 5) 
}) 

私はこれを行うためにdeclaration mergingを使用しようとしましたが、それはTS2403: Subsequent variable declarations must have the same type. Variable 'context' must be of type 'any', but here has type '{ foo: number; }'. で失敗します。

01:
declare module 'ava' { 
    interface ContextualTestContext { 
     context: { 
     foo: number, 
     } 
    } 
} 

test.beforeEach((t) => { 
    t.context = { foo: 5 } 
}) 

test('Is context typed', (t) => { 
    // uncaught ypo 
    t.is(t.context.fooo, 5) 
}) 

すべての時間そうのようなコンテキストをキャストせずにこれを行う方法はあります

答えて

0

コンテキストの入力は、次のバージョンのavaで可能です。

import * as ava from 'ava'; 

function contextualize<T>(getContext:() => T): ava.RegisterContextual<T> { 
    ava.test.beforeEach(t => { 
     Object.assign(t.context, getContext()); 
    }); 

    return ava.test; 
} 

const test = contextualize(() => { 
    return { foo: 'bar' }; 
}); 

test.beforeEach(t => { 
    t.context.foo = 123; // error: Type '123' is not assignable to type 'string' 
}); 

test.after.always.failing.cb.serial('very long chains are properly typed', t => { 
    t.context.fooo = 'a value'; // error: Property 'fooo' does not exist on type '{ foo: string }' 
}); 

test('an actual test', t => { 
    t.deepEqual(t.context.foo.map(c => c), ['b', 'a', 'r']); // error: Property 'map' does not exist on type 'string' 
}); 

あなたが非同期的にあなたのコンテキストを取得した場合、あなたはそれに応じてcontextualizeの型シグネチャを変更する必要があります:そして、あなたはこのような何かができそうでない場合は活字体のコンパイラはt.contextは約束だと思います

function contextualize<T>(getContext:() => Promise<T>): ava.RegisterContextual<T> { 
    ava.test.beforeEach(async t => { 
     Object.assign(t.context, await getContext()); 
    }); 

    return ava.test; 
} 

const test = contextualize(() => { 
    const db = await mongodb.MongoClient.connect('mongodb://localhost:27017') 

    return { db } 
}); 

を、そうではありませんが

0

これを行う一般的な方法はありません。特別な場合は、新しいTestContextを作成することができます。代わりの

export type ContextualTest = (t: ContextualTestContext) => PromiseLike<void> | Iterator<any> | Observable | void; 

使用

export type MyContextualTest<T> = (t : TestContext & {context : T}) => PromiseLike<void> ... 

のようなものとエヴァのこのように適合するものでなければならない、あなた自身のtest機能、宣言します。これは、そう、ほとんどテストされていない

interface MyTestFunction<T> { 
    (name : string, run : MyContextualTest<T>) 
} 

import {test as avaTest} from 'ava'; 
const test : MyTestFunction<IMyContext> = avaTest; 

を何か問題があれば教えてください。

+1

@despairblueあなたのユースケースの問題を提起できますか?おそらく 'test()'シグネチャでジェネリックを受け入れることができます。 –

+0

@MarkWubbenありがとう、私はここでそれを開いたhttps://github.com/avajs/ava/issues/1291 – despairblue

+0

技術的に動作する@ FinnO、私は 'test'関数自体で定義された関数を使用したい場合(' test.beforeEach')すべての宣言を再定義する必要があります。約1700ものものがあります。 – despairblue

関連する問題