1

Karma と Jasmine を使用して、Angular 2 で単体テストを実行しようとしています。Karma (または webpack?) で CSS ファイルの読み込みに問題があるようです。コンポーネントをテストできるように、CSS ファイルが機能する必要があります。

これが私のカルマ設定です...

module.exports = function(config) {
    config.set({
        basePath: '',

        frameworks: ['jasmine'],

        files: [
            'src/tests/tests.ts',
            'src/tests/Routing.spec.ts'
        ],

        exclude: [
        ],

        preprocessors: {
            'src/tests/tests.ts': ['webpack'],
            'src/tests/Routing.spec.ts': ['webpack', 'sourcemap']
        },

        mime: {
            'text/x-typescript': ['ts','tsx']
        },

        webpack: {
            devtool: 'inline-source-map',

            resolve: {
                extensions: ['', '.ts', '.js']
            },

            module: {
                loaders: [
                    {
                        test: /\.js$/,
                        loader: 'babel-loader',
                        query: {
                            presets: ['es2015']
                        }
                    },
                    {
                        test: /\.ts$/,
                        loaders: ['awesome-typescript-loader']
                    }
                ]
            }
        },

        webpackMiddleware: {
            // webpack-dev-middleware configuration
            noInfo: true
        },

        plugins: [
            require("karma-webpack"),
            require("karma-jasmine"),
            require("karma-chrome-launcher"),
            require("karma-sourcemap-loader"),
            require("karma-spec-reporter")
        ],

        reporters: ['spec'],

        port: 9876,

        colors: true,

        // level of logging
        // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
        logLevel: config.LOG_INFO,

        // enable / disable watching file and executing tests whenever any file changes
        autoWatch: true,

        // start these browsers
        // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
        browsers: ['Chrome'],

        // Continuous Integration mode
        // if true, Karma captures browsers, runs the tests and exits
        singleRun: false
    });
};

次のエラーが表示されます。

Failed: Uncaught (in promise): Failed to load css/PageGridStyle.css

Failed: This test module uses the component PageGridComponent which is using a "templateUrl" or "stylesUrl", but they were never compiled. Please call "TestBed.compileComponents" before your test.

これは私の実際のテストです (TestBed.compileComponents を呼び出していることがわかります):

TestBed.initTestEnvironment(
    BrowserDynamicTestingModule,
    platformBrowserDynamicTesting()
);

describe('component: DetailsComponent', function () {
    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [
                CommonModule,
                RouterModule,
                SharedModule,
                RouterTestingModule.withRoutes([
                    { path: 'benefits/details', component: DetailsComponent },
                    { path: '', component: HomeComponent }
                ])
            ],
            declarations: [
                HomeComponent,
                DetailsComponent
            ]
        });
    });

    beforeEach(async(() => {
        TestBed.compileComponents();
    }));

    it('Home should auto nav to Profile Details', async(inject([Router, Location], (router: Router, location: Location) => {
        let detailsComponent = TestBed.createComponent(DetailsComponent);
        detailsComponent.detectChanges();

        //fixture.debugElement.query(By.css('a')).nativeElement.click();
        detailsComponent.whenStable().then(() => {
            expect(location.path()).toEqual('/benefits/details');
            console.log('after expect');
        });
    })));

});

これは私のコンポーネントです:

@Component({
    template: `
    <div>
        <page-grid [PageConfig]="_page"></page-grid>
    </div>
    `
})
export class DetailsComponent {
    private _page;
    constructor() {
        this._page = DetailsQuestions;
    }
}

PageGridComponent (CSS ファイルを含むもの)

@Component({
    selector: 'page-grid',
    template: `
    <div>
        ...
    </div>
    `,
    styleUrls: ['./css/PageGridStyle.css']
})
export class PageGridComponent {
    ...
}

TL;DR

Karma が CSS ファイルを読み込めません。それで、何が起こっているのですか?CSS ファイルが読み込まれないのはなぜですか?

4

0 に答える 0