1 Star 0 Fork 0

魏sir / grafana-sdk-mocks

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Grafana SDK Mocks for Plugins

This package facilitates writing Grafana plugins in TypeScript.

  • provides Typescript typings for the most common Grafana classes.
  • provides some simple fakes and util files to test plugins with Karma

Setup

With npm, Grunt, karma and mocha.js

This shows how to setup a project with Grunt as the build system, using npm to install the packages (yarn is very similar), karma for unit testing and mocha with expect.js for unit testing.

  1. npm install --save systemjs lodash moment (lodash and moment are optional)
  2. npm install --save-dev grunt grunt-contrib-clean grunt-contrib-copy grunt-contrib-watch grunt-typescript karma karma-expect karma-mocha karma-chrome-launcher karma-sinon karma-systemjs karma-phantomjs-launcher plugin-typescript q sinon
  3. Install this package: npm install --save-dev grafana/grafana-sdk-mocks
  4. Here is an example Gruntfile for building TypeScript. It expects the TypeScript files to be located in a src subdirectory.
module.exports = function(grunt) {
  require('load-grunt-tasks')(grunt);

  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-typescript');
  grunt.loadNpmTasks('grunt-contrib-watch');

  grunt.initConfig({
    clean: ['dist'],

    copy: {
      dist_js: {
        expand: true,
        cwd: 'src',
        src: ['**/*.ts', '**/*.d.ts'],
        dest: 'dist'
      },
      dist_html: {
        expand: true,
        flatten: true,
        cwd: 'src/partials',
        src: ['*.html'],
        dest: 'dist/partials/'
      },
      dist_statics: {
        expand: true,
        flatten: true,
        src: ['src/plugin.json', 'LICENSE', 'README.md'],
        dest: 'dist/'
      }
    },

    typescript: {
      build: {
        src: ['dist/**/*.ts', '!**/*.d.ts'],
        dest: 'dist',
        options: {
          module: 'system',
          target: 'es5',
          rootDir: 'dist/',
          declaration: true,
          emitDecoratorMetadata: true,
          experimentalDecorators: true,
          sourceMap: true,
          noImplicitAny: false,
        }
      }
    },

    watch: {
      files: ['src/**/*.ts', 'src/**/*.html', 'src/plugin.json', 'README.md'],
      tasks: ['default'],
      options: {
        debounceDelay: 250,
      },
    }
  });

  grunt.registerTask('default', [
    'clean',
    'copy:dist_js',
    'typescript:build',
    'copy:dist_html',
    'copy:dist_statics'
  ]);
};
  1. Here is an example karma.conf.js file. It assumes that test files are located in the specs subdirectory. It includes config for lodash, momentjs and q (promises).
'use strict';
module.exports = function(config) {
    config.set({
      frameworks: ['systemjs', 'mocha', 'expect', 'sinon'],

      files: [
        'specs/*.ts',
        { pattern: 'src/**/*.ts', included: false },
        { pattern: 'node_modules/grafana-sdk-mocks/**/*.ts', included: false },
        { pattern: 'node_modules/grafana-sdk-mocks/**/*.js', included: false },
        { pattern: 'node_modules/typescript/lib/typescript.js', included: false },
        { pattern: 'node_modules/lodash/lodash.js', included: false },
        { pattern: 'node_modules/moment/moment.js', included: false },
        { pattern: 'node_modules/q/q.js', included: false },
      ],

      systemjs: {
      //   // SystemJS configuration specifically for tests, added after your config file.
      //   // Good for adding test libraries and mock modules
        config: {
          // Set path for third-party libraries as modules
          paths: {
            'systemjs': 'node_modules/systemjs/dist/system.js',
            'system-polyfills': 'node_modules/systemjs/dist/system-polyfills.js',
            'lodash': 'node_modules/lodash/lodash.js',
            'moment': 'node_modules/moment/moment.js',
            'q': 'node_modules/q/q.js',
            'typescript': 'node_modules/typescript/lib/typescript.js',
            'plugin-typescript': 'node_modules/plugin-typescript/lib/plugin.js',
            'app/': 'node_modules/grafana-sdk-mocks/app/',
          },

          map: {
              'plugin-typescript': 'node_modules/plugin-typescript/lib/',
              'typescript': 'node_modules/typescript/',
              'app/core/utils/kbn': 'node_modules/grafana-sdk-mocks/app/core/utils/kbn.js'
          },

          packages: {
            'plugin-typescript': {
                'main': 'plugin.js'
            },
            'typescript': {
                'main': 'lib/typescript.js',
                'meta': {
                    'lib/typescript.js': {
                        'exports': 'ts'
                    }
                }
            },
            'app': {
              'defaultExtension': 'ts',
              'meta': {
                '*.js': {
                  'loader': 'typescript'
                }
              }
            },
            'src': {
              'defaultExtension': 'ts',
            },
            'specs': {
              'defaultExtension': 'ts',
              'meta': {
                '*.js': {
                  'loader': 'typescript'
                }
              }
            },
          },

          transpiler: 'plugin-typescript',
        }
      },

      reporters: ['dots'],

      logLevel: config.LOG_INFO,

      browsers: ['PhantomJS']
    });
};

Typings

Use the following triple slash directive to use Grafana classes in your code. The directive will point the TypeScript compiler at the mocks package so that it can find the files it needs to build. Place the directive at the top of all your TypeScript files:

///<reference path="../node_modules/grafana-sdk-mocks/app/headers/common.d.ts" />

update

  • 1.0.1 add echarts and highcharts module

Commands to build and test

  • grunt to build. This will create the dist subdirectory, copy the TypeScript files there, transpile them to JavaScript as well as copying html files, the License file, the Readme and plugin.json to dist.
  • grunt watch runs grunt when a file is changed. Useful while developing.
  • karma start --single-run runs the unit tests in the specs subdirectory.
  • karma start is a test watcher for unit tests. It will rerun the tests when a file is changed.
The MIT License (MIT) Copyright (c) 2016 Grafana Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Mocks package for the Grafana SDK to be used when developing TypeScript plugins for Grafana. Includes typings so that the plugin can be built and mocks for the Grafana SDK so that Karma tests will work. 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/Mr_V/grafana-sdk-mocks.git
git@gitee.com:Mr_V/grafana-sdk-mocks.git
Mr_V
grafana-sdk-mocks
grafana-sdk-mocks
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891