Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to run test with asynchronous server #2

Open
woprzech opened this issue Jan 5, 2018 · 2 comments
Open

Unable to run test with asynchronous server #2

woprzech opened this issue Jan 5, 2018 · 2 comments
Assignees
Labels

Comments

@woprzech
Copy link
Contributor

woprzech commented Jan 5, 2018

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)
@woprzech woprzech added the bug label Jan 5, 2018
@antusus
Copy link
Contributor

antusus commented Aug 29, 2018

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.

@antusus antusus self-assigned this Aug 29, 2018
@marmatys
Copy link
Contributor

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();
    });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants