Pragmatists / ng-test-runner

MIT License
24 stars 1 forks source link

Unable to run test with asynchronous server #2

Open woprzech opened 6 years ago

woprzech commented 6 years ago

I am unable to run test with respondImmediately : false server configuration - even for simple test case from documentation.

From test from documentation

it('resolves request after respond()', function () {

    // given:
    const server = http({autoRespond: true, respondImmediately: false});
    const app = test(MyModule);
    server.get('/greeting', req => req.sendStatus(200));

    // when:
    const comp = app.run(MyComponent);

    comp.verify(
        expectThat.textOf('.greeting').isEqualTo('Waiting for server response!')
    );

    comp.perform(
        server.respond
    );

    comp.verify(
        expectThat.textOf('.greeting').isEqualTo('Hello from server!')
    );
});

I'm getting an error:

Error: INVALID_STATE_ERR - 0
    at verifyRequestOpened (http://localhost:9876/_karma_webpack_/webpack:/Users/krzysztof/Workspace/ng-test-runner/node_modules/nise/nise.js:762:1)
antusus commented 6 years ago

I started looking into that. It looks like the error is in our code that prepares response from sinon

Example, I'm waiting for response with body that is simple json with attribute message. I've modified the "method" method in server.ts to use sinon like so:

server.respondWith("POST", url, [200, {"Content-Type": "application/json"}, '{"message": "Goodbye Jane!"}']);

And the tests works fine. I think that we have problem with using server.respondWith.

marmatys commented 6 years ago

In following example that uses only sinon and Angular (without ng-test-runner) first test (sinon respondWith with function) fails, but second (sinon respondWith with body) works fine.

import { HttpClient, HttpClientModule } from "@angular/common/http";
import { TestBed } from "@angular/core/testing";
import * as sinon from "sinon";
import { SinonFakeServer, SinonFakeXMLHttpRequest } from "sinon";

describe("Sinon with Angular Async", () => {
    let server: SinonFakeServer;
    let http: HttpClient;

    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [
                HttpClientModule
            ]
        });
        http = TestBed.get(HttpClient);
        server = sinon.fakeServer.create();
        server.respondImmediately = false;
        server.autoRespond = false;
    });

    afterEach(() => {
        server.restore();
    });

    it("sinon respondWith with function", (done: DoneFn) => {
        server.respondWith("GET", /\/test/, (req: SinonFakeXMLHttpRequest) => {
            req.respond(200, {"Content-Type": "application/json"}, JSON.stringify({message: "Text"}));
        });

        http.get("/test").subscribe(
            (resp) => {
                console.log(`Got response ${JSON.stringify(resp)}`);
                expect(resp).toEqual({message: "Text"});
                done();
            }
        );

        server.respond();
    });

    it("sinon respondWith with body", (done: DoneFn) => {
        server.respondWith("GET", /\/test/, JSON.stringify({message: "Text"}));

        http.get("/test").subscribe(
            (resp) => {
                console.log(`Got response ${JSON.stringify(resp)}`);
                expect(resp).toEqual({message: "Text"});
                done();
            }
        );

        server.respond();
    });
});