<commit_before>import {
    join
} from 'path';
import {
    readJson,
    remove
} from 'fs-extra';
import webpack from 'webpack';
import StatsWriterWebpackPlugin from '../src/index';

describe('StatsWriterWebpackPlugin', () => {
    const TMP_DIR = './test/tmp';

    const clean = (stats) => {
        // NOTE (mdreizin): `time` does't exist in `emit` phase
        delete stats.time;

        // NOTE (mdreizin): `stats.json` is emitted, but `stats.json` is saved without it
        stats.assets = [];

        return stats;
    };

    const compile = (dirname, filename = 'stats.json', done) => {
        webpack({
            debug: true,
            profile: true,
            timings: true,
            output: {
                path: dirname
            },
            plugins: [
                new StatsWriterWebpackPlugin(filename)
            ]
        }, (err, stats) => {
            const expected = stats.toJson();

            readJson(join(dirname, filename), (_, actual) => {
                expect(clean(expected)).toEqual(clean(actual));

                done();
            });
        });
    };

    beforeEach(() => spyOn(console, 'log'));

    afterEach(done => remove(TMP_DIR, done));

    describe('#apply()', function() {
        it('should save `stats.json` successfully', done => compile(TMP_DIR, undefined, done));

        it('should save `stats1.json` successfully', done => compile(TMP_DIR, 'stats1.json', done));
    });
});
<commit_msg>test(*): updates tests (#2)<commit_after>import {
    join
} from 'path';
import {
    readJson,
    remove
} from 'fs-extra';
import webpack from 'webpack';
import StatsWriterWebpackPlugin from '../src/index';

describe('StatsWriterWebpackPlugin', () => {
    const TMP_DIR = './test/tmp';

    const clean = stats => {
        // NOTE (mdreizin): `time` does't exist in `emit` phase
        delete stats.time;

        // NOTE (mdreizin): `stats.json` is emitted, but `stats.json` is saved without it
        stats.assets = [];

        return stats;
    };

    const compile = (filename = 'stats.json', options = {}, callback) => {
        const compiler = webpack(Object.assign({}, options, {
            plugins: [
                new StatsWriterWebpackPlugin(filename)
            ]
        }));

        const done = (err1, stats) => {
            expect(err1).toEqual(null);

            const expected = stats.toJson();

            readJson(join(options.output.path, filename), (err2, actual) => {
                expect(err2).toEqual(null);
                expect(clean(expected)).toEqual(clean(actual));

                callback();
            });
        };

        if (options.watchOptions) {
            compiler.watch(options.watchOptions, done);
        } else {
            compiler.run(done);
        }
    };

    beforeEach(() => spyOn(console, 'log'));

    afterEach(done => remove(TMP_DIR, done));

    describe('#apply()', function() {
        let options;

        beforeEach(() => {
            options = {
                debug: true,
                profile: false,
                output: {
                    path: TMP_DIR
                }
            };
        });

        describe('#run()', () => {
            it('should save `stats.json` successfully', done => compile(undefined, options, done));

            it('should save `stats1.json` successfully', done => compile('stats1.json', options, done));

            it('should add missing `timings`', done => {
                options = Object.assign(options, {
                    profile: true
                });

                compile('stats1.json', options, done);
            });
        });

        describe('#watch()', () => {
            beforeEach(() => {
                options = Object.assign(options, {
                    watchOptions: {}
                });
            });

            it('should save `stats.json` successfully', done => compile(undefined, options, done));
        });
    });
});

{'fraction_non_alphanumeric': 0.15031367628607278, 'fraction_numerical': 0.002509410288582183, 'mean_word_length': 1.771905424200278, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2476851', 'n_tokens_mistral': 1105, 'n_tokens_neox': 1051, 'n_words': 247}