LoginSignup
2
2

More than 5 years have passed since last update.

Karma/Jasmineで(jsonの)fixtureを使う

Last updated at Posted at 2016-03-11

一度、うまく行かなかったので整理をするために記事にしました。

ES6+Karma+Jasmine環境のテンプレートの続きです。

jasmine-jquery

KarmaのことはほっといてJasmineのjQuery拡張を使用。

via http://qiita.com/ngyuki/items/d36e500b55e1f627acab

ほぼ上記の記事のとおり。

現在ではkarma-jasmineのjasmineは2.x系のようなので特に問題ない。

karma.conf.js
module.exports = function(config) {
  config.set({
    ...
    files: [
      '/path/to/jquery/dist/jquery.min.js',
      '/path/to/jasmine-jquery/lib/jasmine-jquery.js',
      ...
      { pattern: '/path/to/fixtures/**', included: false }
    ],
    ...
  });
};

dependenciesはこんな感じ

package.json
{
  ...
  "devDependencies": {
    ...
    "jasmine-core": "^2.4.1",
    "jasmine-jquery": "^2.1.1",
    "jquery": "^2.2.1",
    "karma": "^0.13.15",
    "karma-browserify": "^4.4.2",
    "karma-chrome-launcher": "^0.2.2",
    "karma-jasmine": "^0.3.6"
  }
}

試してみる

describe('Sample', () => {

  beforeEach(() => {
    jasmine.getFixtures().fixturesPath = 'base/path/to/fixtures';
  });

  it('can use fixtures', () => {
    let fixture = readFixtures('path/to/fixture');
    expect(fixture).toBe('[{"this":"is","a":"json","fixture":"!"}]')
  });
});

karma-jasmine-jquery

Karma用のjasmine-jqueryプラグインを使用。

karma.conf.js
module.exports = function(config) {
  config.set({
    ...
    frameworks: [..., 'jasmine-jquery', 'jasmine'],
    ...
    files: [
      ...
      { pattern: '/path/to/fixtures/**', included: false }
    ],
    ...
    plugins: [..., 'karma-jasmine-jquery', 'karma-jasmine', ...],
    ...
  });
};

frameworks'jasmine-jquery'。また、公式には記載がないが、plugins'karma-jasmine-jquery'が必要だった。

via http://stacksolve.com/lvO2xkgLp1BY/no-provider-for-jasminejquery.html

package.json
{
  ...
  "devDependencies": {
    ...
    "jasmine-core": "^2.4.1",
    "karma": "^0.13.15",
    "karma-browserify": "^4.4.2",
    "karma-chrome-launcher": "^0.2.2",
    "karma-jasmine": "^0.3.6",
    "karma-jasmine-jquery": "^0.1.1"
  }
}

filesへの記載が少ない分、楽かも。

karma-fixture/karma-json-fixtures-preprocessor

テストスイートはmochaでないとダメな模様。

サンプルのレポジトリ

こうして、まとめてみると大したハマりどころもないが、なぜなのか…

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2