microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
test/resources/recordingsHelper.ts
69lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import * as fs from "fs"; |
| 5 | import * as path from "path"; |
| 6 | |
| 7 | const RECORDINGS_ROOT = path.resolve(__dirname, "processExecutionsRecordings"); |
| 8 | |
| 9 | interface TestUsingRecording { |
| 10 | (expectation: string, recordingNames: string[], assertion?: () => void): Mocha.ITest; |
| 11 | (expectation: string, recordingNames: string[], assertion?: (done: MochaDone) => void): Mocha.ITest; |
| 12 | only(expectation: string, recordingNames: string[], assertion?: () => void): Mocha.ITest; |
| 13 | only(expectation: string, recordingNames: string[], assertion?: (done: MochaDone) => void): Mocha.ITest; |
| 14 | skip(expectation: string, recordingNames: string[], assertion?: () => void): void; |
| 15 | skip(expectation: string, recordingNames: string[], assertion?: (done: MochaDone) => void): void; |
| 16 | } |
| 17 | |
| 18 | export interface IRecordingConsumer { |
| 19 | loadRecordingFromName(recordingName: string): Q.Promise<void>; |
| 20 | loadRecordingFromString(recordingName: string): Q.Promise<void>; |
| 21 | } |
| 22 | |
| 23 | /* This class makes it easy to create a test using a recording. Recommended usage is: |
| 24 | const testWithRecordings = new RecordingsHelper(() => recordingConsumer).test; |
| 25 | testWithRecordings("expects to do some test thing", |
| 26 | [ |
| 27 | "path/to/recording", |
| 28 | "path/to/recording" |
| 29 | ], () => { |
| 30 | // test code here |
| 31 | }); |
| 32 | */ |
| 33 | export class RecordingsHelper { |
| 34 | public test: TestUsingRecording; |
| 35 | |
| 36 | private recordings: { [name: string]: string }; |
| 37 | |
| 38 | constructor(private getRecordingConsumer: () => IRecordingConsumer) { |
| 39 | this.recordings = {}; |
| 40 | this.initializeTest(); |
| 41 | } |
| 42 | |
| 43 | private initializeTest(): void { |
| 44 | this.test = <TestUsingRecording>((testName: string, recordingNames: string[], code: () => Q.Promise<void>): void => { |
| 45 | if (code.length !== 0) { // Check how many arguments the function has |
| 46 | throw new RangeError("(done: mochaDone) parameter is not supported. Please return a promise instead."); |
| 47 | } |
| 48 | const recordingsHelper = this; |
| 49 | recordingNames.forEach(recordingName => { |
| 50 | |
| 51 | let recording: string = this.recordings[recordingName]; |
| 52 | if (!recording) { |
| 53 | recording = fs.readFileSync(path.resolve(RECORDINGS_ROOT, recordingName) + ".json", "utf8"); |
| 54 | this.recordings[recordingName] = recording; |
| 55 | } |
| 56 | |
| 57 | test(`${testName} using recording ${recordingName}`, function () { // We use function () because we need the this pointer |
| 58 | return recordingsHelper |
| 59 | .getRecordingConsumer() |
| 60 | .loadRecordingFromString(recording) |
| 61 | .then(code.bind(this)); |
| 62 | }); |
| 63 | }); |
| 64 | }); |
| 65 | this.test.skip = (expectation: string, recordingNames: string[], assertion?: (done: MochaDone) => void) => { |
| 66 | test.skip(expectation, assertion); |
| 67 | }; |
| 68 | } |
| 69 | } |
| 70 | |