~endlessm/jasmine-gjs/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* global jasmineImporter */

const ConsoleReporter = jasmineImporter.consoleReporter;

describe('Console reporter base class', function () {
    let reporter, timerSpies;
    const jasmineCorePath = 'path/to/jasmine/core/jasmine.js';

    beforeEach(function () {
        timerSpies = {};
        const timerSpy = id => {
            timerSpies[id] = jasmine.createSpyObj('timer', ['start', 'elapsed']);
            return timerSpies[id];
        };
        reporter = new ConsoleReporter.ConsoleReporter({
            timerFactory: timerSpy,
            jasmineCorePath,
        });
    });

    it('can be instantiated', function () {
        reporter = new ConsoleReporter.ConsoleReporter();
    });

    it('starts the main timer when Jasmine starts', function () {
        reporter.jasmineStarted();
        expect(timerSpies['main'].start).toHaveBeenCalled();
    });

    it('purges Jasmine internals from stack traces', function () {
        const fakeStack = [
            `foo${jasmineCorePath}`,
            `bar ${jasmineCorePath}`,
            'line of useful stack trace',
            `baz ${jasmineCorePath}`,
        ].join('\n');
        const stackTrace = reporter.filterStack(fakeStack);
        expect(stackTrace).toMatch('line of useful stack trace');
        expect(stackTrace).not.toMatch(jasmineCorePath);
    });

    describe('started signal', function () {
        beforeEach(function () {
            reporter = new ConsoleReporter.ConsoleReporter();
        });

        it('is emitted when the suite starts', function (done) {
            reporter.connect('started', () => done());
            reporter.jasmineStarted();
        });
    });

    describe('complete signal', function () {
        beforeEach(function () {
            reporter = new ConsoleReporter.ConsoleReporter();
            reporter.jasmineStarted();
        });

        it('is emitted with true when the suite is done', function (done) {
            reporter.connect('complete', (_, success) => {
                expect(success).toBeTruthy();
                done();
            });
            reporter.jasmineDone();
        });

        it('is emitted with false if there are spec failures', function (done) {
            reporter.connect('complete', (_, success) => {
                expect(success).toBeFalsy();
                done();
            });
            reporter.specDone({status: 'failed', failedExpectations: []});
            reporter.jasmineDone();
        });

        it('is emitted with false if there are suite failures', function (done) {
            reporter.connect('complete', (_, success) => {
                expect(success).toBeFalsy();
                done();
            });
            reporter.specDone({status: 'passed'});
            reporter.suiteDone({failedExpectations: [{message: 'bananas'}]});
            reporter.jasmineDone();
        });
    });

    it('times individual suites', function () {
        const suiteInfo = {id: 'foo'};
        reporter.suiteStarted(suiteInfo);
        expect(timerSpies['suite:foo'].start).toHaveBeenCalled();

        timerSpies['suite:foo'].elapsed.and.returnValue(800);
        reporter.suiteDone(suiteInfo);
        expect(suiteInfo.time).toBe(800);
    });

    it('times individual specs', function () {
        const specInfo = {
            id: 'foo',
            status: 'passed',
        };
        reporter.specStarted(specInfo);
        expect(timerSpies['spec:foo'].start).toHaveBeenCalled();

        timerSpies['spec:foo'].elapsed.and.returnValue(800);
        reporter.specDone(specInfo);
        expect(specInfo.time).toBe(800);
    });

    it('starts a timer', function () {
        reporter.startTimer('foobar');
        expect(timerSpies['foobar'].start).toHaveBeenCalled();
    });

    it('gets the elapsed time from a timer', function () {
        reporter.startTimer('foobar');
        timerSpies['foobar'].elapsed.and.returnValue(500);
        expect(reporter.elapsedTime('foobar')).toBe(500);
    });
});